UIScrollView、PanGestureRecognizer 和 iOs 4
Posted
技术标签:
【中文标题】UIScrollView、PanGestureRecognizer 和 iOs 4【英文标题】:UIScrollView, PanGestureRecognizer and iOs 4 【发布时间】:2012-01-10 18:16:14 【问题描述】:我有一个可滚动的视图,我需要在不滚动主视图的情况下触发的手势识别器。
我用过这段代码:
UISwipeGestureRecognizer *upRecognizer =
[[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(upSwipeHandle:)];
upRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[upRecognizer setNumberOfTouchesRequired:2];
[controller.view addGestureRecognizer:upRecognizer];
[[controller.scrollView panGestureRecognizer]
requireGestureRecognizerToFail:upRecognizer];
但它似乎只适用于 ios 5。如何在 iOS 4 中使用相同的识别器?
【问题讨论】:
【参考方案1】:我通过使用 UIGestureRecognizer 委托方法解决了我的问题。
添加
upRecognizer.delegate = self;
还有这个方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
return YES;
return NO;
【讨论】:
【参考方案2】:我遇到了同样的问题。我在 4.3 模拟器中运行了类似的代码,但该代码失败了,因为 scrollView.panGestureRecognizer 仅在 iOS 5.0+ 中可用。
我通过向 UIScrollView 添加一个类别来解决它。
@interface UIScrollView(panGestureRecognizer)
@end
@implementation UIScrollView(panGestureRecognizer)
// iOS5 has a panGestureRecognizer property
// Add a getter for iOS 4.3+
- (UIPanGestureRecognizer *)panGestureRecognizer
for (UIGestureRecognizer *gestureRecognizer in self.gestureRecognizers)
if ([gestureRecognizer isKindOfClass: [UIPanGestureRecognizer class]])
return (UIPanGestureRecognizer *)gestureRecognizer;
return nil;
@end
【讨论】:
以上是关于UIScrollView、PanGestureRecognizer 和 iOs 4的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView里面的UIScrollView:滚动
问题:UIScrollview 弹跳使父 UIScrollview 弹跳
UIScrollView 嵌入其他 UIScrollView