Scrolling in a UIScrollView without triggering touchesCancelled

送分小仙女□ 提交于 2020-01-01 08:35:31

问题


Overview

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.

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.

 

The Issue

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.

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.

 

Failed Attempts to Fix

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.

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).

 

Where Do I Go From Here?

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?


回答1:


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;
}

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;    
    }
}



回答2:


- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated 

this might do the trick.... any touchesMoved can trigger a method that would scroll with a velocity propotional to the distance between the center of the screen(or anywhere else) and last recorded position of touch until a touchesEnded.



来源:https://stackoverflow.com/questions/5261543/scrolling-in-a-uiscrollview-without-triggering-touchescancelled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!