问题仅在于 UIScrollView,我也对此大发雷霆:http: //i.stack.imgur.com/dqx3d.png
[更新 1] 这里的解决方案:
情况:一个 UIViewController 的视图有一个 UIScrollView 作为子视图(滚动视图分页被禁用)。
我用来将手势附加到 UIView 的代码:
UISwipeGestureRecognizer *swipeLeftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreenLeft:)];
[swipeLeftRecognizer setNumberOfTouchesRequired:2.0f];
swipeLeftRecognizer.delegate = self;
[swipeLeftRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeLeftRecognizer];
[swipeLeftRecognizer release];
要启用 iOS 4.3,我只需将 UIViewController 添加为UIGestureRecognizerDelegate
然后,我使用以下委托方法拦截并允许同时识别滚动视图的 panGesture 和视图的滑动一次。这里的代码:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
[更新 2]
要使用两根手指禁用 UIScrollView 平移,我认为您必须制作滚动视图,这是 UISCrollView 类的自定义子类并更改 panGesture 检测器的某些行为,但我没有尝试这样做。相反,我选择了一个更懒的解决方案,基本上我根据 UISwipeGestureRecognizer 的当前状态启用/禁用 scrollView 滚动功能。此外,为了防止在另一个方向上的双重触摸移动,我为此附加了另一个识别器。
您必须为滑动检测器创建两个属性。
@property (nonatomic,assign) UISwipeGestureRecognizer *swipeRightRecognizer;
@property (nonatomic,assign) UISwipeGestureRecognizer *swipeLeftRecognizer;
然后我这样编码:
[self setSwipeRightRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:nil action:nil]];
[swipeRightRecognizer setNumberOfTouchesRequired:2.0f];
swipeRightRecognizer.delegate = self;
[swipeRightRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self addObserver:self forKeyPath:@"swipeRightRecognizer.state" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
[self.view addGestureRecognizer:swipeRightRecognizer];
[swipeRightRecognizer release];
[self setSwipeLeftRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreenLeft:)]];
[swipeLeftRecognizer setNumberOfTouchesRequired:2.0f];
swipeLeftRecognizer.delegate = self;
[swipeLeftRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self addObserver:self forKeyPath:@"swipeLeftRecognizer.state" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
[self.view addGestureRecognizer:swipeLeftRecognizer];
[swipeLeftRecognizer release];
然后添加这个方法:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (self.swipeRightRecognizer.state == UIGestureRecognizerStateFailed) {
self.scrollView.scrollEnabled = YES;
return;
}
if ([self.swipeRightRecognizer numberOfTouches] != 2.0f) {
self.scrollView.scrollEnabled = YES;
}
else{
self.scrollView.scrollEnabled = NO;
}
}
并更新我在之前的“[UPDATE]”中发布的现有方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
if ([gestureRecognizer numberOfTouches] != 2.0f) {
self.scrollView.scrollEnabled = YES;
}
else{
self.scrollView.scrollEnabled = NO;
}
return YES;
}
return NO;
}
最后,删除 dealloc 中的观察者:
[self removeObserver:self forKeyPath:@"swipeRightRecognizer.state"];
[self removeObserver:self forKeyPath:@"swipeLeftRecognizer.state"];
我敢打赌有一个更清洁的解决方案,但它的工作原理......
希望能帮助到你 ;)