我添加了一个-(void) detectTouch: (UIPanGestureRecognizer *) event
forUIScrollView
并检测用户移动手指的角度。我的任务是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
在用户移动触摸时启用滚动?