繁体   English   中英

在UIButton上长按手势识别器?

[英]Long press gesture recognizer on UIButton?

我正在开发Minesweeper游戏,我想在用户长按游戏板的磁贴时添加标志。 我已经实现了以下代码:

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

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

当然,self是我的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];
}

}

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[[UILongPressGestureRecognizer块]:无法识别的选择器发送到实例0x8cf2b00'

从该错误中可以很明显地看出,由于[UILongPressGestureRecognizer block]应用程序崩溃了。 UILongPressGestureRecognizer没有名为block的方法,所以它崩溃了。

- (void)longPressTap:(Tile *)sender {
}

如您所料,此方法中的实现sender不是Tile对象,它实际上是UILongPressGestureRecognizer

预期的方法是

- (void)longPressTap:(UILongPressGestureRecognizer *)sender
{
}

我了解你的问题。 我现在可以谈谈“瓷砖”是什么。

- (void)longPressTap:(Tile *)sender

使用这个可能会有所帮助

- (void)longPressTap:(UILongPressGestureRecognizer *)sender

并且不要使用直接图块。 在此处直接使用Tile对象,并使此对象成为全局对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM