【问题标题】:Long press gesture recognizer on UIButton?UIButton上的长按手势识别器?
【发布时间】:2014-06-18 08:42:56
【问题描述】:

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

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

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTap:)];
            longPress.minimumPressDuration = 1.0f;
            [self.button addGestureRecognizer:longPress];

在self中,方法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 编程,所以如果有人可以帮助我并解释我做错了什么,我将非常感激。

【问题讨论】:

    标签: ios objective-c uibutton long-press


    【解决方案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];
    }
    

    }

    【讨论】:

    • 很好的解决方案!这解决了 UILongPressGestureRecognizer 在内部触摸时再次触发的问题。
    【解决方案2】:

    我了解您的问题。我现在可以讨论什么是“平铺”吗?

    - (void)longPressTap:(Tile *)sender
    

    使用它可能会有所帮助

    - (void)longPressTap:(UILongPressGestureRecognizer *)sender
    

    并且不要使用直接平铺。在此处直接使用 Tile 对象,并将其设为全局对象 ..

    【讨论】:

    • 我将 Tile 作为参数传递,因为我不知道如何识别用户选择的 Tile。所以现在我有: - (void)longPressTap:(UILongPressGestureRecognizer *)sender 但是你能给我一个 sn-p 来将瓷砖标记为标志/空白吗? ty
    • 尝试将长按手势添加到 Tile 对象而不是按钮,并使用 Tile 对象作为发件人参数,可能会起作用
    【解决方案3】:

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

    从这个错误中可以清楚地看出,由于[UILongPressGestureRecognizer block],应用程序正在崩溃。 UILongPressGestureRecognizer 没有名为 block 的方法,所以它正在崩溃。

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

    正如你所期望的,在这个方法中实现sender不是Tile对象,它实际上是UILongPressGestureRecognizer

    预期的方法是

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

    【讨论】:

    • 是的,这正是我认为的错误,但是将 UILongPressGestureRecognizer 作为 longPressTap 的参数传递:如何检查 Tile 用户选择的内容并将其标记为标志/空白?对不起,菜鸟问题,但我正在努力理解
    • 使用[sender locationInView]从那里找到触摸的位置,你应该有一个逻辑来获取关联的Tile
    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 2015-03-19
    相关资源
    最近更新 更多