longPress 时启用 UIPanGestureRecognizer
Enable UIPanGestureRecognizer when did longPress
我想在 customView
上启用 UIPanGestureRecognizer
,而 customView
longPress
。
(我希望当你longPress
customView
时,customView
会切换到"move mode",你可以通过拖动移动customView
。)
但是在这段代码中,只调用了longPressAction:
。 panAction:
没有打电话。
如何修复它以启用 PanAction:
?
- (void)viewDidLoad
{
[self.view addSubview:customView];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[customView addGestureRecognizer:longPressRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[customView addGestureRecognizer:panRecognizer];
}
- (void)longPressAction:(UILongPressGestureRecognizer *)recognizer
{
if ([recognizer state] == UIGestureRecognizerStateBegan) {
CustomView *customView = (CustomView *)recognizer.view;
customView.panRecongnizerEnabled = YES; //panRecongnizerEnabled is CustomView's property
}
if ([recognizer state] == UIGestureRecognizerStateEnded) {
CustomView *customView = (CustomView *)recognizer.view;
customView.panRecongnizerEnabled = NO;
}
}
- (void)panAction:(UIPanGestureRecognizer *)recognizer
{
CustomView *customView = (CustomView *)recognizer.view;
if (customCell.panRecongnizerEnabled == NO) return;
NSLog(@"running panAction");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
您的 ViewController 需要符合 UIGestureRecognizerDelegate
。我怀疑您要么已经这样做了 要么 否则 shouldRecognizeSimultaneouslyWithGestureRecognizer
将没有任何意义。但是您肯定缺少的是将 gestureRecognizer
的委托设置为您的 viewController:
longPressRecognizer.delegate = self;
panRecognizer.delegate = self;
现在您应该同时接收到长按和平移。
注意:我在没有任何 customView 的情况下进行了测试,只是将它们添加到 self.view
。至少在那种情况下,上面的代码按预期工作。
我知道这个问题已经关闭了一段时间,但我最近不得不做类似的事情,并且有一个更简洁的解决方案。 UILongPressGestureRecognizer
就是您执行此操作所需的全部内容。
Long-press gestures are continuous. The gesture begins
(UIGestureRecognizerStateBegan) when the number of allowable fingers
(numberOfTouchesRequired) have been pressed for the specified period
(minimumPressDuration) and the touches do not move beyond the
allowable range of movement (allowableMovement). The gesture
recognizer transitions to the Change state whenever a finger moves,
and it ends (UIGestureRecognizerStateEnded) when any of the fingers
are lifted.
正因为如此,对于你所需要的,就没有必要了UIPanGestureRecognizer
。只需添加 UILongPressGestureRecognizer
并将其附加到您的主视图。
- (void)viewDidLoad {
[super viewDidLoad];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
// set this to your desired delay before the pan action will start.
longPressRecognizer.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressRecognizer];
}
然后,实现 longPressAction 方法,不仅可以查看 UIGestureRecognizerStateBegan
或 UIGestureRecognizerStateEnded
状态,还可以查看 UIGestureRecognizerStateChanged
状态:
- (void)longPressAction:(UIGestureRecognizer *)recognizer
{
if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged ) {
CGPoint touchPoint = [recognizer locationInView:self.view];
[self.viewToMove setCenter: touchPoint];
NSLog(@"running panAction");
}
}
在我的示例中,我只是移动了一个虚拟视图来跟踪用户手指的中心,但是您可以将任何逻辑放入 UIPanGestureRecognizer
的代码中。只需将它放在 if 块中,它会大大简化您的代码,并且您无需处理两个手势识别器之间的交互。当用户只是在屏幕上移动手指(但没有长按)时,您的代码还会导致对 panAction
方法的许多不必要的调用。
您可以通过长按后启用平移来实现。无论如何,您必须保存一个标志才能知道是否调用了 longPress。为了在长按后启用平移手势,您可以使用平移手势委托,如下所示:
let previewLongPress = UILongPressGestureRecognizer(target: self, action: #selector(onLongPressed(sender:)))
let previewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(onPanGesture(sender:)))
previewPanGesture?.delegate = self
您的委托应如下所示:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer == previewPanGesture && otherGestureRecognizer == previewLongPress
}
现在您可以在长按手势后使用平移手势。
希望对其他人有所帮助。
我想在 customView
上启用 UIPanGestureRecognizer
,而 customView
longPress
。
(我希望当你longPress
customView
时,customView
会切换到"move mode",你可以通过拖动移动customView
。)
但是在这段代码中,只调用了longPressAction:
。 panAction:
没有打电话。
如何修复它以启用 PanAction:
?
- (void)viewDidLoad
{
[self.view addSubview:customView];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[customView addGestureRecognizer:longPressRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[customView addGestureRecognizer:panRecognizer];
}
- (void)longPressAction:(UILongPressGestureRecognizer *)recognizer
{
if ([recognizer state] == UIGestureRecognizerStateBegan) {
CustomView *customView = (CustomView *)recognizer.view;
customView.panRecongnizerEnabled = YES; //panRecongnizerEnabled is CustomView's property
}
if ([recognizer state] == UIGestureRecognizerStateEnded) {
CustomView *customView = (CustomView *)recognizer.view;
customView.panRecongnizerEnabled = NO;
}
}
- (void)panAction:(UIPanGestureRecognizer *)recognizer
{
CustomView *customView = (CustomView *)recognizer.view;
if (customCell.panRecongnizerEnabled == NO) return;
NSLog(@"running panAction");
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
您的 ViewController 需要符合 UIGestureRecognizerDelegate
。我怀疑您要么已经这样做了 要么 否则 shouldRecognizeSimultaneouslyWithGestureRecognizer
将没有任何意义。但是您肯定缺少的是将 gestureRecognizer
的委托设置为您的 viewController:
longPressRecognizer.delegate = self;
panRecognizer.delegate = self;
现在您应该同时接收到长按和平移。
注意:我在没有任何 customView 的情况下进行了测试,只是将它们添加到 self.view
。至少在那种情况下,上面的代码按预期工作。
我知道这个问题已经关闭了一段时间,但我最近不得不做类似的事情,并且有一个更简洁的解决方案。 UILongPressGestureRecognizer
就是您执行此操作所需的全部内容。
Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.
正因为如此,对于你所需要的,就没有必要了UIPanGestureRecognizer
。只需添加 UILongPressGestureRecognizer
并将其附加到您的主视图。
- (void)viewDidLoad {
[super viewDidLoad];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
// set this to your desired delay before the pan action will start.
longPressRecognizer.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressRecognizer];
}
然后,实现 longPressAction 方法,不仅可以查看 UIGestureRecognizerStateBegan
或 UIGestureRecognizerStateEnded
状态,还可以查看 UIGestureRecognizerStateChanged
状态:
- (void)longPressAction:(UIGestureRecognizer *)recognizer
{
if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged ) {
CGPoint touchPoint = [recognizer locationInView:self.view];
[self.viewToMove setCenter: touchPoint];
NSLog(@"running panAction");
}
}
在我的示例中,我只是移动了一个虚拟视图来跟踪用户手指的中心,但是您可以将任何逻辑放入 UIPanGestureRecognizer
的代码中。只需将它放在 if 块中,它会大大简化您的代码,并且您无需处理两个手势识别器之间的交互。当用户只是在屏幕上移动手指(但没有长按)时,您的代码还会导致对 panAction
方法的许多不必要的调用。
您可以通过长按后启用平移来实现。无论如何,您必须保存一个标志才能知道是否调用了 longPress。为了在长按后启用平移手势,您可以使用平移手势委托,如下所示:
let previewLongPress = UILongPressGestureRecognizer(target: self, action: #selector(onLongPressed(sender:)))
let previewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(onPanGesture(sender:)))
previewPanGesture?.delegate = self
您的委托应如下所示:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return gestureRecognizer == previewPanGesture && otherGestureRecognizer == previewLongPress
}
现在您可以在长按手势后使用平移手势。
希望对其他人有所帮助。