简体   繁体   English

如何检测用户是否只想滚动而不是在UIScrollView内点击UIImageView?

[英]How can I detect if the user just wanted to scroll, and not tap on an UIImageView inside a UIScrollView?

I've implemented an UIScrollView which contains a grid of UIImageView objects. 我实现了一个UIScrollView,其中包含一个UIImageView对象的网格。 For both, the superview and it's subviews, userInteractionEnabled is YES. 对于超级视图及其子视图,userInteractionEnabled为YES。 As the user scrolls and his/her finger touches an UIImageView, it imediately recognizes a tap on that, which is not what I want ;) 当用户滚动并且他/她的手指触摸UIImageView时,它立即识别出对此的轻击,这不是我想要的;)

Inside of that UIImageView objects I have this method: 在该UIImageView对象内部,我具有以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == self && [touch tapCount] >= 1) {
        NSLog(@"Cooooool! The user touched me!");
    }
}

Is there a way how I could recognize something like "oh no, the user actually wanted to scroll only, so don't take that touch too serious!"? 有没有一种方法可以识别类似“哦,不,用户实际上只想滚动,所以请不要太认真!”? Basically, what I need is the behavior of the home screen, where a finger may touch an icon, but when the user starts scrolling and lifts the finger off, the touched app will not be launched. 基本上,我需要的是主屏幕的行为,即手指可能触摸图标,但是当用户开始滚动并抬起手指时,将不会启动触摸的应用程序。

UIScrollView has already implemented that behavior for you. UIScrollView已经为您实现了该行为。 If the user is quick enough to drag, your touchesBegan:withEvent: method will simply never be called. 如果用户足够快地拖动,则永远不会调用您的touchesBegan:withEvent:方法。

If, on the other hand, the user is slow and the scroll view forwards the touch to your UIImageView subclass, but then only realizes the user meant to scroll, it will call your custom class' -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event method. 另一方面,如果用户很慢并且滚动视图将触摸转发到UIImageView子类,但随后仅意识到用户打算滚动,它将调用您的自定义类' -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event方法。

Just implement it, and you'll be notified of cancelled touches. 只需实施它,您就会收到取消触摸的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM