在UIScrollView中滚动而不触发touchesCancelled [英] Scrolling in a UIScrollView without triggering touchesCancelled

查看:110
本文介绍了在UIScrollView中滚动而不触发touchesCancelled的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款iPhone游戏,其代码从另一个开发人员继承而来。游戏网格是具有1000x1000的contentSize的UIScrollView。网格包含通过OpenGL的Texture2D类在屏幕上绘制的几个元素。

I'm working on an iPhone game whose code I inherited from another developer. The gaming grid is a UIScrollView having a contentSize of 1000x1000. The grid contains several elements drawn on the screen via OpenGL's Texture2D class.

UIScrollView在与手指移动方向相反的方向上滚动。这背后的想法是模拟用你的手指通过元素筛选的行为,因为你的触摸的路径中的每个元素都应该受到影响/操纵。

The UIScrollView scrolls in the direction opposite of that in which your finger is moving. The idea behind this is to simulate the act of "sifting" through the elements with your finger, as each element in the path of your touch is supposed to be affected/manipulated.

问题是,UIScrollView的滚动触发touchesCancelled,从而结束touchesMoved调用,游戏依赖于知道要操作的元素。即使我的手指移动没有在视觉上触发视图滚动,将我的手指移动超过10像素的距离触发了已触摸的方法,从而结束未来的触摸移动电话,直到我抬起手指并开始新的触摸事件。

The problem is that the scrolling of the UIScrollView triggers touchesCancelled, thus ending the touchesMoved calls that the game relies on to know which elements to manipulate. Even if my finger movement doesn't visually trigger the view to scroll, moving my finger more than 10 pixels in distance triggers the touchesCancelled method, thus ending future touchesMoved calls until I lift my finger and start a new touch event.

我相信这是触发touchesCancelled方法的滚动事件,因为如果我设置 self.scrollingEnabled = NO; 在视图上,移动手指在屏幕上继续触发触摸移动,因为它应该,无论我移动我的手指在屏幕上多远,touchesEnded获得调用预期,当我抬起手指(在我完成筛选元素),并且我的触摸/滑动的路径中的每个元素确实根据需要被操纵。但是,设置此属性自然会阻止游戏网格的所需滚动。

I am confident that it's the scrolling event that is triggering the touchesCancelled method, because if I set self.scrollingEnabled = NO; on the view, moving the finger on the screen continues to trigger touchesMoved as it should no matter how far I move my finger on the screen, touchesEnded gets called as expected when I lift my finger (after I'm done sifting through the elements), and each element in the path of my touch/swipe is indeed manipulated as desired. However, setting this property naturally prevents the desired scrolling of the game grid.

 

我已经尝试在 initWithFrame 中设置 self.canCancelContentTouches = NO; code>视图的方法,但 touchesCancelled 仍然被触发,奇怪的。不知道为什么!也许我已经在错误的地方,但我不太确定,甚至会是我的问题的解决方案,因为文档意味着这样的设置会阻止滚动反正:如果这个属性的值是否,滚动视图不会滚动,无论手指移动一旦内容视图开始跟踪。显然,这不是我的行为,所以该属性似乎不是我感兴趣的毕竟。虽然我仍然困惑为什么它仍然滚动,并调用 touchesCancelled ,但我离题。

I have tried setting self.canCancelContentTouches = NO; in the initWithFrame method of the view, but touchesCancelled is still being triggered, oddly enough. Not sure why that is! Maybe I've got it in the wrong spot, but I'm not so sure that would even be the solution to my issue because the docs imply that such a setting would prevent scrolling anyway: "If the value of this property is NO, the scroll view does not scroll regardless of finger movement once the content view starts tracking." Clearly that's not the behavior I'm after, so that property doesn't seem to be what I'm interested in after all. Though I'm still perplexed at why it still scrolled and called touchesCancelled, but I digress.

也添加了方法 - (BOOL)touchesShouldCancelInContentView:(UIView *)view 但它不会被调用,即使 self.canCancelContentTouches = NO; 因为文档说,将canCancelTouches属性设置为NO将确实阻止touchesShouldCancelInContentView方法被调用:滚动视图不调用此方法,如果值canCancelContentTouches属性为NO。。虽然文档似乎不清楚是否从此方法返回NO也会防止滚动像 canCancelContentTouches = NO; 设置。如果不是,那么这应该是理想的解决方案。再次,虽然,我不知道为什么这种方法甚至不被调用,因为docs只提到一个属性/设置阻止它被调用,它仍然没有被调用,即使该属性没有设置为NO (因此默认为YES)。

I've also added the method - (BOOL)touchesShouldCancelInContentView:(UIView *)view but it's not getting called, even without setting self.canCancelContentTouches = NO; as the docs say that setting the canCancelTouches property to NO would indeed prevent the touchesShouldCancelInContentView method from being called: "The scroll view does not call this method if the value of the canCancelContentTouches property is NO.". Though the docs seem unclear as to whether or not returning NO from this method would also prevent scrolling like the canCancelContentTouches = NO; setting would. If not, then this aught to be the ideal solution. Again though, I have no idea why this method isn't even getting called, as the docs only mention that one property/setting that prevents it from being called, and it still didn't get called even with that property not set to NO (thus defaulting to YES).

 

所以现在我失去了。事实是,我仍然需要touchesMoved继续在视图开始滚动后调用。这是可能吗?如果没有,我的选项在这里?

So now I'm at a loss. The fact is I still need touchesMoved to continue being called after the view has started scrolling. Is that at all possible? If not, what are my options here?

推荐答案

我有完全相同的问题(浪费了很多时间)。
然后我尽快设置 cancelsTouchesInView 属性(我在 viewDidLoad )。 ..它为我工作(如下所示

I had exactly the same problem (and wasted a lot of time). Then I tried to set the cancelsTouchesInView property as soon as possible (I did it in viewDidLoad)... and it worked for me (as shown here)!

- (void)viewDidLoad {
    [super viewDidLoad];
    /* other initializations */
    scrollView.panGestureRecognizer.cancelsTouchesInView = NO;
}

这适用于iOS 5.在iOS 5之前,您需要遍历所有识别器,找到panGestureRecognizer,然后设置属性。
所以这应该是一个通用的解决方案:

This works in iOS 5. Before iOS 5 you need to go through all the recognizers, find the panGestureRecognizer, and then set the property. So this should be a universal solution:

- (void)viewDidLoad {
    [super viewDidLoad];
    /* other initializations */
    for (UIGestureRecognizer *gesture in scrollView.gestureRecognizers){
        if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]){
        gesture.cancelsTouchesInView = NO;    
    }
}

这篇关于在UIScrollView中滚动而不触发touchesCancelled的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆