3

ユーザーが指を動かしている角度を検出する-(void) detectTouch: (UIPanGestureRecognizer *) eventための を追加しました。UIScrollView私の仕事はUIScrollView、ユーザーが指を0〜30度の間で動かしているときにのみ水平にスクロールすることです(水平の直線を描いていることを確認するためだけです)。それ以外の場合は、UIScrollViewスクロールを無効にする必要があります。

タッチの始点と終点を使って三角形を描くことで角度を検出しています。

問題:UIScrollView角度が 30 度未満のときにスクロールを有効にしましたが、最初は機能しません。スクロールを有効にしまし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

UIScrollViewは、.scrollEnabledをYESに設定した後、新しいタッチでのみスクロールを受け入れるように見えるので、次のようにします。

  • .scrollEnabledをNOに設定します
  • タッチを追跡します(例:touchesMoved ::)
  • 30度の条件が満たされているかどうかを確認します
  • 動きを使用してUIScrollView.contentOffsetを調整します
于 2012-10-23T09:05:05.923 に答える