UILongPressGestureReconizer标签? - 苹果手机

时间:2012-07-29 21:45:44

标签: iphone objective-c ios uibutton uigesturerecognizer

我正在建立一个像这样的LPGR,我很想知道我是否可以在每个LPGR中创建一个标签。我需要这样做,所以我知道我的所有按钮都被按下了......

UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
longpressGesture.minimumPressDuration = 2;
[longpressGesture setDelegate:self];
[pushButton addGestureRecognizer:longpressGesture];

我的方法如下......

- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(@"longPressHandler");
}

我知道你不能通过选择器传递参数,所以我想知道我是否可以为LPGR分配一个标签,或者如果在方法中我可以获取使用LPGR的按钮的标签?这是否可能>?

编辑:

NSInteger *tag = [gestureRecognizer.view.tag];
NSLog(@"%@ longPressHandler",tag);

2 个答案:

答案 0 :(得分:2)

UIGestureRecognizer有一个属性view,它是手势识别器所附加的视图。

因此,在您的处理程序方法中,gestureRecognizer.view是LPGR附加到的按钮,而gestureRecognizer.view.tag是按钮的标记。

<强>增加: 示例代码:

- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
    NSLog(@"longPressHandler");
    NSInteger tag = gestureRecognizer.view.tag;
    NSLog(@"%d longPressHandler",tag);
}

答案 1 :(得分:0)

您只需创建UILongPressGestureRecognizer的子类并为其添加tag属性即可。您还可以使用associated objects添加带有类别的属性。

相关问题