Long press gesture recognizer on UIButton?

倾然丶 夕夏残阳落幕 提交于 2019-12-25 02:41:07

问题


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

In self, the method 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--;
        }
}

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'

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.


回答1:


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

}




回答2:


I understand your problem . Can I now what is "Tile" in argument.

- (void)longPressTap:(Tile *)sender

use this may be helpfull

- (void)longPressTap:(UILongPressGestureRecognizer *)sender

and don't use direct Tile. Use Tile object direct here and make global object of this ..




回答3:


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

From this error it is very clear app is crashing because of [UILongPressGestureRecognizer block]. UILongPressGestureRecognizer doesnt have a method called block so it is crashing.

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

As you expected in the implementation sender in this method is not Tile object, it is actually UILongPressGestureRecognizer.

The expected method is

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


来源:https://stackoverflow.com/questions/24280978/long-press-gesture-recognizer-on-uibutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!