手势问题:UISwipeGestureRecognizer + UI滑块
遇到与手势相关的问题。我实现了 UISwipeGestureRecognizer 来获取向左和向右滑动事件,并且工作正常。然而,我面临的问题是,我在同一视图中的 UISlider 玩得不好。滑块的滑动动作被误认为是向左/向右滑动。
任何人以前遇到过这个问题,有任何想法如何纠正它吗?
非常感谢。
这是视图控制器中包含的代码:
- (void)viewDidLoad {
[super viewDidLoad];
//Setup handling of LEFT and RIGHT swipes
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Swipe Right");
//Do stuff
}
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Swipe Left");
//Do stuff
}
}
Got a gesture related problem. I implemented UISwipeGestureRecognizer to get swipe left and right events and that is working fine. However the problem I'm facing is that the UISlider's I have in the same view are not playing nice. The sliding motion of the sliders is being mistaken as a swipe left/right.
Any one experienced this problem before, got any ideas how to correct it?
Many thanks.
Here is the code contained within the view controller:
- (void)viewDidLoad {
[super viewDidLoad];
//Setup handling of LEFT and RIGHT swipes
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Swipe Right");
//Do stuff
}
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Swipe Left");
//Do stuff
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理此问题的最简单方法可能是防止手势识别器看到滑块上的触摸。您可以通过将自己设置为委托来做到这一点,然后实现
如果这不起作用,则滑块可能实际上具有接收触摸的子视图,因此您可以沿着
superview
链向上走,测试沿途的每一个景色。The simplest way to handle this is probably to prevent the gesture recognizer from seeing touches on your slider. You can do that by setting yourself as the delegate, and then implementing
If this doesn't work, it's possible the slider actually has subviews that receive the touch, so you could walk up the
superview
chain, testing each view along the way.斯威夫特 4.0 版本。 不要忘记 UIGestureRecognizerDelegate。
Swift 4.0 version. Don't forget the UIGestureRecognizerDelegate.
在莉莉做出上述回应之前,我最终完成了这项工作。这是我使用的代码,但莉莉的看起来更干净(你还没有测试莉莉的):
I ended up getting this working just before Lily responded above. Here is the code I used, but Lily's looks cleaner (haven't tested Lily's thou):