开发者

UILongPressGestureRecognizer not working, but swapping it for a UITapGestureRecognizer works fine. Why?

开发者 https://www.devze.com 2023-04-10 07:31 出处:网络
I have a UIImageView with a UILongPressGestureRecognizer attached that never seems to detect a long press gesture no matter how I configure the gesture recognizer. However, if I swap it out for a UITa

I have a UIImageView with a UILongPressGestureRecognizer attached that never seems to detect a long press gesture no matter how I configure the gesture recognizer. However, if I swap it out for a UITapGestureRecognizer, that works just fine. What could possibly be going on?

This is how I'm configuring my UILongPressGestureRecognizer:

UIImageView* cellView = (UIImageView*)[view viewWithTag:5];
UILongPressGestureRecognizer* longPressGestureRec =
    [[UILongPressGestureReco开发者_Python百科gnizer alloc] initWithTarget:self action:@selector(cellLongPress:)];
longPressGestureRec.numberOfTapsRequired = 1;
longPressGestureRec.numberOfTouchesRequired = 1;
longPressGestureRec.minimumPressDuration = 0.4;
[cellView addGestureRecognizer:longPressGestureRec];
[longPressGestureRec release];

This is what cellLongPress looks like:

-(void)cellLongPress:(UILongPressGestureRecognizer*)recognizer
{
    // This never gets called.
    NSLog(@"someone long pressed me");
}

Pretty straightforward, right? No luck so far getting it to work, though. Any ideas?


The numberOfTapsRequired is set to 1 which means the user has to tap once before starting the long press (finger down, finger up, finger down for 0.4 seconds, gesture recognized).

Change numberOfTapsRequired to 0 (which is the default).

For that property, the documentation just says:

The number of taps on the view required for the gesture to be recognized.

But in the comments in UILongPressGestureRecognizer.h, it says:

The number of full taps required before the press for gesture to be recognized

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号