问题描述
我想在 customView
执行 longPress
时在 customView
上启用 UIPanGestureRecognizer
.
(我希望当你longPress
customView
时,customView
会切换到移动模式",你可以移动customView
代码> 通过拖动.)
I'd like to enable UIPanGestureRecognizer
on customView
when the customView
did longPress
.
(I wish when you longPress
customView
, the customView
will switch to "move mode", and you can move the customView
by drag.)
但在这段代码中,只有 longPressAction:
被调用.panAction:
没有调用.
如何修复它以启用 PanAction:
?
But in this code, only longPressAction:
called. panAction:
did not called.
How do I fix it to enable 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:
Your ViewController needs to conform to the UIGestureRecognizerDelegate
. I suspect you either already did that or otherwise the shouldRecognizeSimultaneouslyWithGestureRecognizer
would not make any sense. But what you are definitely missing is setting the gestureRecognizer
´s delegate to your viewController:
longPressRecognizer.delegate = self;
panRecognizer.delegate = self;
现在您应该同时接收长按和平移.
Now you should be receiving both long press and pan simultaneous.
注意:我在没有任何 customView 的情况下进行了测试,只是将它们添加到 self.view
中.至少在那种情况下,上面的代码按预期工作.
Note: I tested without any customView, just added them to self.view
. At least in that case, the code above worked as expected.
这篇关于长按时启用 UIPanGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!