gpt4 book ai didi

ios - UILongPressGestureRecognizer 不起作用

转载 作者:行者123 更新时间:2023-12-01 19:04:18 34 4
gpt4 key购买 nike

我在 UIImageView 中添加了 4 个手势识别器,单击、双击和 pin 手势工作正常。但是,长按手势不起作用。这是为什么?

        _imageView.userInteractionEnabled = YES ;

//single tap
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(singleTapAction:) ] ;
singleTap.numberOfTapsRequired = 1 ;
singleTap.numberOfTouchesRequired = 1 ;

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)] ;
doubleTap.numberOfTouchesRequired = 1 ;
doubleTap.numberOfTapsRequired = 2 ;
[singleTap requireGestureRecognizerToFail:doubleTap] ;

//pin gesture
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)] ;

//long press gesture
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:) ] ;
[longPress requireGestureRecognizerToFail:singleTap ] ;
longPress.minimumPressDuration = 1 ;
longPress.numberOfTouchesRequired = 1 ;
longPress.numberOfTapsRequired = 1 ;

[_imageView addGestureRecognizer:longPress] ;
[_imageView addGestureRecognizer:pin] ;
[_imageView addGestureRecognizer:singleTap] ;
[_imageView addGestureRecognizer:doubleTap] ;


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}

大家好,我加了 shouldRecognizeSimultaneouslyWithGestureRecognizer方法并设置长按 gesutre 委托(delegate),但这仍然不起作用。

最佳答案

你的错误可能在这里

[longPress requireGestureRecognizerToFail:longPress ] ;

为什么 longPress 要求本身失败?删除它。

您不了解 requireGestureRecognizerToFail 命令。它在手势需要其他未能触发时使用。如果 longPress 未触发,则触发 tapGesture。
在你的情况下捏失败->长按失败->双击失败->单击
同时删除这一行: longPress.numberOfTapsRequired = 1 ;

注释掉您的代码并使用此代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imageView.userInteractionEnabled = YES;
self.imageView.multipleTouchEnabled = YES;
UIGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1 ;
longPress.numberOfTouchesRequired = 1 ;
[longPress requireGestureRecognizerToFail:pinchGesture];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
doubleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;
[doubleTap requireGestureRecognizerToFail:longPress];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
singleTap.numberOfTouchesRequired = 1;
singleTap.numberOfTapsRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];

[self.imageView addGestureRecognizer:pinchGesture];
[self.imageView addGestureRecognizer:longPress];
[self.imageView addGestureRecognizer:doubleTap];
[self.imageView addGestureRecognizer:singleTap];

}

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gesture{
NSLog(@"Pinch");
}


- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture{
NSLog(@"LongPress");
}

- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture{
NSLog(@"Double Tap");
}

- (void)handleSingleTap:(UITapGestureRecognizer *)gesture{
NSLog(@"Single Tap");
}

关于ios - UILongPressGestureRecognizer 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20762513/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com