kus*_*uya 5 objective-c uigesturerecognizer ios
我想启用UIPanGestureRecognizer的customView时候customView做longPress。
(我想你的时候longPress customView,在customView将切换到“移动模式”,你可以移动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;
}
Run Code Online (Sandbox Code Playgroud)
您的 ViewController 需要符合UIGestureRecognizerDelegate. 我怀疑你要么已经这样做了,要么shouldRecognizeSimultaneouslyWithGestureRecognizer没有任何意义。但你肯定缺少的是设置gestureRecognizer\xc2\xb4s 委托给你的 viewController:
longPressRecognizer.delegate = self;\npanRecognizer.delegate = self;\nRun Code Online (Sandbox Code Playgroud)\n\n现在您应该同时接收到长按和平移。
\n\n注意:我测试时没有任何自定义视图,只是将它们添加到self.view. 至少在这种情况下,上面的代码按预期工作。
| 归档时间: |
|
| 查看次数: |
763 次 |
| 最近记录: |