在UIButton上长按手势识别器?
我正在开发扫雷游戏,当用户长时间敲击游戏板的一个图块时,我想添加这个标志。 我已经实现了这个代码:
对于游戏板中的每个button:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTap:)]; longPress.minimumPressDuration = 1.0f; [self.button addGestureRecognizer:longPress];
在自我中, longPressTap方法:
- (void)longPressTap:(Tile *)sender { if (sender.block.marking == MARKING_FLAGGED) { // if already a flag I mark as a blank tile, with color defined for gameboard sender.backgroundColor = UIColorFromRGB(0x067AB5); sender.block.marking = MARKING_BLANK; self.flagCount++; } else{ // if it's not a flag I mark as a flag and set the flag image for the tile [sender setBackgroundImage:[UIImage imageNamed:IMAGE_NAME_FLAG] forState:UIControlStateNormal]; sender.block.marking = MARKING_FLAGGED; self.flagCount--; } }
当然,我自己是我的UIGestureRecognizerDelegate 。 但是当我尝试长时间按压一个贴图时,应用程序崩溃并给出这个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer block]: unrecognized selector sent to instance 0x8cf2b00'
我该怎么办? 我刚刚开始使用Obj-C编程,所以如果有人能帮我解释我做错了什么,我会非常感激。
- (void)showOptions:(UILongPressGestureRecognizer*)sender{ UIButton *btn = (UIButton*)sender.view; NSLog(@"view tag %d",sender.view.tag); if (sender.state == UIGestureRecognizerStateEnded) { } else if (sender.state == UIGestureRecognizerStateBegan) { [self.bubbleDelegate showOptionsForMessage:btn]; }
}
我明白你的问题。 我可以现在什么是“瓷砖”的论点。
- (void)longPressTap:(Tile *)sender
使用这可能是有帮助的
- (void)longPressTap:(UILongPressGestureRecognizer *)sender
并且不要使用直接的Tile。 直接在这里使用Tile对象,并使这个全局对象。
*由于未捕获exception'NSInvalidArgumentException',原因:' – [UILongPressGestureRecognizer块]:无法识别的select器发送到实例0x8cf2b00'
从这个错误是非常清楚的应用程序崩溃,因为[UILongPressGestureRecognizer block]
。 UILongPressGestureRecognizer
没有一个叫block
的方法,所以它崩溃了。
- (void)longPressTap:(Tile *)sender { }
正如你所期望的,在这个方法中sender
不是Tile
对象,它实际上是UILongPressGestureRecognizer
。
预期的方法是
- (void)longPressTap:(UILongPressGestureRecognizer *)sender { }