UILongPressGestureRecognizer关闭UIButton突出显示

use*_*428 19 iphone ios4 ios

我有一个UILongPressGestureRecognizer添加到UIButton.当我按下UIButton时,它会按预期突出显示.但是,当调用UILongPressGestureRecognizer选择器时,突出显示将被关闭.

    UILongPressGestureRecognizer *longpressGesture = 
     [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(longPressHandler:)];
    longpressGesture.minimumPressDuration = 5;
    [longpressGesture setDelegate:self];
    [self.myUIButton addGestureRecognizer:longpressGesture];
    [longpressGesture release];

    - (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer {
    NSLog(@"longPressHandler");
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,按住按钮5秒后调用选择器.在调用选择器之前,该按钮会突出显示,但在调用选择器时会突然显示该按钮,即使我仍然按下该按钮.

任何人都可以解释为什么会发生这种情况,以及如何预防?我希望按下按钮在整个按下时保持高亮显示.谢谢.

use*_*428 37

经过进一步的研究,我发现这是由于手势识别器的默认行为,一旦识别出手势就会取消层次结构中的触摸.因此,一旦手势识别器识别出手势,它就会取消对UI按钮的触摸,然后不再突出显示,因为它不再有触摸事件.

可以使用cancelsTouchesInView属性更改此行为

longpressGesture.cancelsTouchesInView = NO;
Run Code Online (Sandbox Code Playgroud)

将此设置为NO会将触摸传递到响应者链.