在UIButton上长按手势识别器? [英] Long press gesture recognizer on UIButton?

查看:210
本文介绍了在UIButton上长按手势识别器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Minesweeper游戏,我想在用户长按游戏板的图块时添加标志. 我已经实现了这段代码:

I'm working on the Minesweeper game, I want to add the flag when user long tap on a tile of the gameboard. I've implemented this code:

对于游戏板上的每个按钮:

For every button in gameboard:

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 . 但是,当我尝试长按磁贴时,应用程序崩溃并出现以下错误:

And of course self is my UIGestureRecognizerDelegate. But when I try to long press on a tile, the app crash and give this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer block]: unrecognized selector sent to instance 0x8cf2b00'

我该怎么办?我刚开始使用Obj-C编程,所以如果有人可以帮助我并解释我做错了什么,我将非常感激.

What should I do? I'm at the very beginning with Obj-C programming so if someone can help me and explain what I did wrong I'll be very grateful.

推荐答案

- (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];
}

}

这篇关于在UIButton上长按手势识别器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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