UIGestureRecognizers的自定义操作(带自定义参数) [英] Custom actions for UIGestureRecognizers (with custom parameters)

查看:84
本文介绍了UIGestureRecognizers的自定义操作(带自定义参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题的简短版本:

我无法弄清楚如何让我的UITapGestureRecognizer的操作采取其他参数,以及实际使用它们。

I cannot figure out how to make the "action" for my UITapGestureRecognizer take additional parameters, and actually use them.

以下是我的问题的概述:

我是试图让我的iPad应用程序记录(使用NSLog)UITouch的坐标,只要他们按下我的应用程序的UIButtons之一就会发生。触摸的位置需要相对于触摸的按钮。

I am trying to make it so that my iPad app records (with NSLog) the coordinates of the UITouch that occurs whenever they press one of my app's UIButtons. The location of the touch needs to be relative to the button that was touched.

我做了什么:

我已经实现了一个UITapGestureRecognizer并将其添加到我的每个按钮中。我的问题是要使用的操作,因为它需要为每个按钮动态。

I have implemented a UITapGestureRecognizer and added it to each of my buttons. My problem is with the action to use, since it needs to be dynamic for each and every button.

我目前有这个代码:

 UITapGestureRecognizer *iconClickRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(logIcon:withTag:)];
 [iconClickRecognizer setNumberOfTapsRequired:1];
 [iconClickRecognizer setNumberOfTouchesRequired:1];
 [iconClickRecognizer setDelegate:self];
 [[self.view viewWithTag:1] addGestureRecognizer:iconClickRecognizer];

 [iconClickRecognizer release];

当我知道这有效时,我将使用for循环将iconClickRecognizer添加到所有按标签的按钮。

When I know that this works, I will use a for-loop to add the iconClickRecognizer to all of the buttons by their tag.

此处显示 logIcon:(int)withTag 方法:

-(void)logIcon:(UIGestureRecognizer *)gestureRecognizer withTag:(int)tag {
  NSLog(@"tag X: %f", [gestureRecognizer locationInView:(UIView*)[self.view viewWithTag:tag]].x);
  NSLog(@"tag Y: %f", [gestureRecognizer locationInView:(UIView*)[self.view viewWithTag:tag]].y);
}

什么不行:

当我将标签硬编码到logIcon方法时,它会正确记录信息。但是,我不知道如何使此方法动态,并且实际使用tag参数。

When I hard-code a tag into logIcon method, it records the information correctly. However, I do not know how to make this method dynamic, and actually use the "tag" parameter.

我们非常感谢任何帮助。

Any help would be greatly appreciated.

谢谢,
Alex

Thanks, Alex

推荐答案

UIGestureRecognizer类的文档指定操作必须采用以下形式:

The docs for UIGestureRecognizer class specify that the action must be of the form:

- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

不是您的表格:

- (void)logIcon:(UIGestureRecognizer *)gestureRecognizer withTag:(int)tag

因此,您可以询问gestureRecognizer在整个窗口中的位置,然后与您的按钮进行比较,或者您可以浏览按钮并询问手势相对于每个按钮的位置。

So you could ask the gestureRecognizer where it is on the whole window, then compare to your buttons, or you could walk through your buttons and ask the gesture where it is with respect to each button.

可能最好的是将UIButton子类化并使每个按钮本身成为目标;然后你就确切地知道你所处的观点。

Probably best would be to subclass UIButton and make each button itself the target; then you know exactly what view you're in.

这篇关于UIGestureRecognizers的自定义操作(带自定义参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆