gpt4 book ai didi

cocoa-touch - 手势识别器在 iOS 4.3 中停止工作

转载 作者:行者123 更新时间:2023-12-01 09:07:21 24 4
gpt4 key购买 nike

我的手势识别代码在 iOS 4.2 中运行良好,但在 iOS 4.3 中无法运行。我在 iOS 4.2 到 4.3 的手势识别器中找不到任何记录在案的更改,但我已在 iPad 和模拟器中确认我的代码不再工作。

这就是我正在做的:

在我的 View Controller 的 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 不能。

最佳答案

问题只出在 UIScrollView 上,我也对此大发雷霆:http://i.stack.imgur.com/dqx3d.png

[更新 1] 这里的解决方案:

情况:一个 UIViewController 的 View 有一个 UIScrollView 作为 subview ( ScrollView 分页被禁用)。

我用来将手势附加到 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

然后,我使用以下委托(delegate)方法拦截并允许同时识别 ScrollView 的 panGesture 和 View 的滑动。代码如下:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}

return NO;

}

[更新 2]

要禁用用两根手指平移 UIScrollView,我认为您必须创建 ScrollView ,这是 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"];

我敢打赌,有一个更清洁的解决方案,但它确实有效.. .

希望对你有帮助;)

关于cocoa-touch - 手势识别器在 iOS 4.3 中停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5294584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com