Ios 在UILongPressGestureRecognitor上,如何检测生成事件的对象?
我有一个带有几个按钮的视图。我已经成功地使用UILongPressGestureRecognitor实现了以下选项:Ios 在UILongPressGestureRecognitor上,如何检测生成事件的对象?,ios,uigesturerecognizer,Ios,Uigesturerecognizer,我有一个带有几个按钮的视图。我已经成功地使用UILongPressGestureRecognitor实现了以下选项: - (void)longPress:(UILongPressGestureRecognizer*)gesture { if ( gesture.state == UIGestureRecognizerStateEnded ) { NSLog(@"Long Press"); } } 在这个方法中,我需要知道哪个UIButton收到了longpres
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(@"Long Press");
}
}
在这个方法中,我需要知道哪个UIButton收到了longpress,因为我需要做一些不同的事情,这取决于哪个按钮收到了longpress
希望答案不是将长按发生位置的坐标映射到按钮边界的问题,而是宁愿不去那里
有什么建议吗
谢谢 这在
手势视图中可用。如果您的视图包含多个子视图(如许多按钮),您可以确定点击了什么:
// Get the position of the point tapped in the window co-ordinate system
CGPoint tapPoint = [gesture locationInView:nil];
UIView *viewAtBottomOfHeirachy = [self.window hitTest:tapPoint withEvent:nil];
if ([viewAtBottomOfHeirachy isKindOfClass:[UIButton class]])
是否将长点击手势控制器添加到将UIButtons作为子视图的UIView?如果是这样的话,按照@Magic Bullet Dave的方法可能是一条可行之路
另一种方法是将UIButton子类化,并为每个UIButton添加一个LongTapGestureRecograiser。然后你可以让你的按钮做你喜欢的。例如,它可以向视图控制器发送标识自身的消息。下面的代码片段演示了该子类的方法
- (void) setupLongPressForTarget: (id) target;
{
[self setTarget: target]; // property used to hold target (add @property and @synthesise as appropriate)
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:button action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void) longPress: (UIGestureRecognizer*) recogniser;
{
if (![recogniser isEnabled]) return; // code to prevent multiple long press messages
[recogniser setEnabled:NO];
[recogniser performSelector:@selector(setEnabled:) withObject: [NSNumber numberWithBool:YES] afterDelay:0.2];
NSLog(@"long press detected on button");
if ([[self target] respondsToSelector:@selector(longPressOnButton:)])
{
[[self target] longPressOnButton: self];
}
}
在视图控制器中,可能有如下代码:
- (void) viewDidLoad;
{
// set up buttons (if not already done in Interface Builder)
[buttonA setupLongPressForTarget: self];
[buttonB setupLongPressForTarget: self];
// finish any other set up
}
- (void) longPressOnButton: (id) sender;
{
if (sender = [self buttonA])
{
// handle button A long press
}
if (sender = [self buttonB])
{
// handle button B long press
}
// etc.
}
if([[self-target]respondsToSelector:@selector(longPressOnButton:)]){[[self-target]longPressOnButton:self];}此if块未编译…给出的错误为:没有已知的实例方法作为。。。