Scrolling in a UIScrollView without triggering touchesCancelled

后端 未结 2 2493
独厮守ぢ
独厮守ぢ 2021-02-20 18:27

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 co

相关标签:
2条回答
  • 2021-02-20 19:27
    - (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 讨论(0)
  • 2021-02-20 19:28

    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;    
        }
    }
    
    0 讨论(0)
提交回复
热议问题