我的手势识别代码在iOS 4.2中运行良好,但在iOS 4.3中它不起作用。我无法在iOS 4.2到4.3中找到手势识别器的任何记录更改,但我已在iPad和模拟器中确认我的代码不再有效。
这就是我在做的事情:
在我的视图控制器的ViewDidLoad方法中,我把:
UISwipeGestureRecognizer *swipeUpGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreenLeft:)] autorelease];
swipeUpGesture.numberOfTouchesRequired = 2;
swipeUpGesture.direction = (UISwipeGestureRecognizerDirectionLeft);
[scrollView addGestureRecognizer:swipeUpGesture];
在iOS 4.2中,这可以按预期工作,但在iOS 4.3中,即使用两根手指轻扫,也不会调用swipedScreenLeft。一切都编译并运行,没有任何错误或警告。
是否有任何可能阻止此手势识别器在iOS 4.3中工作的内容,即使它在iOS 4.2中正常工作?
另外我注意到在iOS 4.2下,如果我用两根手指触摸屏幕但没有做出正确的手势,没有任何事情会发生,但在iOS 4.3中,如果我用两根手指触摸屏幕,它就好像我只是用一根手指触摸。就好像iOS 4.3无法识别我的应用程序中的多点触控事件。
另一个注意事项:我的tapGestureRecognizer在iOS 4.3中运行良好,只是swipeGestureRecognizer没有。
答案 0 :(得分:8)
问题出在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
然后,我使用以下委托方法拦截并允许使用视图的滑动同时识别scrollView的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;
}
}
并更新我在之前的“[更新]”中发布的现有方法:
- (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"];
我打赌有一个更清洁的解决方案,但它确实有效..
希望它有所帮助;)
答案 1 :(得分:1)
如果您使用UIImageView并且想要对其进行双击,则应启用用户交互。我浪费了很多时间来发现这一点。
答案 2 :(得分:0)
也许我的代码可以帮助您解决问题。 我已经在iOS 4.3的iPad上测试了代码 而且效果很好
UISwipeGestureRecognizer *oSwipeLeft = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(mPreviousPage:) ] autorelease];
oSwipeLeft.numberOfTouchesRequired = 2;
oSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:oSwipeLeft];
UISwipeGestureRecognizer *oSwipeRight = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(mNextPage:) ] autorelease];
oSwipeRight.numberOfTouchesRequired = 2;
oSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:oSwipeRight];
UISwipeGestureRecognizer *oSwipeUp = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(mPreviousPage:) ] autorelease];
oSwipeUp.numberOfTouchesRequired = 2;
oSwipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:oSwipeUp];