开发者

Scrolling in a UIScrollView without triggering touchesCancelled

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 st开发者_StackOverflow中文版ill 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜