3

我添加了一个-(void) detectTouch: (UIPanGestureRecognizer *) eventforUIScrollView并检测用户移动手指的角度。我的任务是UIScrollView仅当用户在 0 - 30 度之间移动手指时才水平滚动(只是为了确保他正在绘制水平直线),否则我必须禁用UIScrollView滚动。

我通过使用触摸起点和终点绘制三角形来检测角度。

问题:我在角度 < 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在用户移动触摸时启用滚动?

4

1 回答 1

0

由于在将 .scrollEnabled 设置为 YES 后 UIScrollView 似乎只接受新的触摸滚动,因此我将执行以下操作:

  • 将 .scrollEnabled 设置为 NO
  • 跟踪触摸(例如使用 touchesMoved::)
  • 检查您是否满足 30 度条件
  • 使用移动调整 UIScrollView.contentOffset
于 2012-10-23T09:05:05.923 回答