Iphone UILongPressGestiureReconizer标签?-苹果手机
我正在像这样设置一个LPGR,我想知道是否可以在每个LPGR中创建一个标记。我需要这样做,这样我就知道我所有的按钮都被按下了Iphone UILongPressGestiureReconizer标签?-苹果手机,iphone,objective-c,ios,uibutton,uigesturerecognizer,Iphone,Objective C,Ios,Uibutton,Uigesturerecognizer,我正在像这样设置一个LPGR,我想知道是否可以在每个LPGR中创建一个标记。我需要这样做,这样我就知道我所有的按钮都被按下了 UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; longpressGesture.minimumPressDuration = 2;
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的按钮的标记?这有可能吗>
编辑:
您只需创建一个子类
uilongpressurerecognizer
,并向其添加一个tag
属性。您还可以使用添加带有类别的属性。UIgestureRecognitor
有一个属性视图
,该视图是手势识别器附加到的视图
因此,在处理程序方法中,gesturecognizer.view
是LPGR附加到的按钮,gesturecognizer.view.tag
是按钮的标记
添加:
示例代码:
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(@"longPressHandler");
NSInteger tag = gestureRecognizer.view.tag;
NSLog(@"%d longPressHandler",tag);
}
代码中有几个语法错误:
tag
是属性,因此没有方括号,NSInteger
是标量类型,而不是对象。我已经在我的答案中添加了正确的代码。
- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
NSLog(@"longPressHandler");
NSInteger tag = gestureRecognizer.view.tag;
NSLog(@"%d longPressHandler",tag);
}