2012-10-23 40 views
3

我爲UIScrollView添加了一個-(void) detectTouch: (UIPanGestureRecognizer *) event並檢測用戶正在移動他的手指的角度。我的任務是水平滾動UIScrollView只有當用戶在0 - 30度之間移動手指(只是爲了確保他畫的是水平直線),否則我必須禁用UIScrollView滾動。objective C啓用UIScrollView滾動,同時UIGestureRecognizerStateChanged

我使用觸摸起點和終點繪製三角形來檢測角度。

問題:當角度爲< 30度時,我啓用了UIScrollView滾動,但這不是第一次工作。雖然我啓用了使用scrollEnabled = YES的滾動,但它只在用戶停止觸摸屏幕時才起作用(將手指從屏幕上移開)。

下面的代碼我用來

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self PanGesture:self.view callBack:@selector(detectTouch:) delegate:self]; 
    incrementer = 0;  
} 

-(void) detectTouch: (UIPanGestureRecognizer *) event{ 
    // Calculating point A on gesture starts 
    if(event.state == UIGestureRecognizerStateBegan){ 
     pointA.x = fabs([event translationInView:event.view].x); 
     pointA.y = fabs([event translationInView:event.view].y); 

     NSLog(@"A: %f, %f", pointA.x, pointA.y); 
    } 

    incrementer += 1; 

    // Start calculating Point B, Point C on calling this function 3 times 
    if(incrementer >= 3){ 

     // Calculating point C 
     pointC.x = fabs([event translationInView:event.view].x); 
     pointC.y = fabs([event translationInView:event.view].y); 

     NSLog(@"C: %f, %f", pointC.x, pointC.y); 

     // calculate pointB using A, C 
     pointB.x = fabs(pointC.x); 
     pointB.y = fabs(pointA.y); 

     NSLog(@"B: %f, %f", pointB.x, pointB.y); 

     float X = pointB.x - pointA.x; 
     float Y = pointC.y - pointB.y; 
     float angle = (atan(fabs(Y)/fabs(X)) * 180/M_PI); 

     if(angle > 30){ 
      // This disable is not working on while user is moving the finger 
      self.myScrollView.scrollEnabled = NO; 
      NSLog(@"UIScroll Disabled"); 
     }else{ 
      // This enable is not working on while user is moving the finger 
      self.myScrollView.scrollEnabled = YES; 
      NSLog(@"UIScroll Enabled"); 
     } 

     incrementer = 0; 
    } 
} 

如何啓用UIScrollView滾動,同時用戶移動觸摸?

+0

你在哪裏創建你的平移手勢識別器??我沒有看到任何地方.. –

回答

0

由於UIScrollView中似乎你設置.scrollEnabled後是隻接受一個新的觸摸滾動,我將做到以下幾點:

  • 集.scrollEnabled爲NO
  • 軌道觸摸(例如用touchesMoved ::)
  • 檢查你30度條件是否滿足
  • 使用的移動來調整UIScrollView.contentOffset