ユーザーが指を動かしている角度を検出する-(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
ユーザーがタッチを動かしているときにスクロールを有効にするにはどうすればよいですか?