如何在UIView上设置UIPanGestureRecognizer区域的限制?

时间:2017-07-16 13:50:48

标签: ios objective-c

-(void)move:(UIPanGestureRecognizer *)recognizer
{
    UIGestureRecognizerState state = [recognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [recognizer translationInView:recognizer.view];
        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
        [recognizer setTranslation:CGPointZero inView:recognizer.view];
    }
}

1 个答案:

答案 0 :(得分:-1)

您可以使用多种方法;

首先,您可以在主视图中添加透明子视图,并将平移手势分配给该子视图。因此,只会识别子视图上的平移手势,您可以直接翻译主视图。

像这样:

UIGestureRecognizerState state = [recognizer state];

if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
    CGPoint translation = [recognizer translationInView:self.panView];

    [self.mainView setTransform:CGAffineTransformTranslate(self.mainView.transform, translation.x, translation.y)];

    [recognizer setTranslation:CGPointZero inView:self.panView];
}

PanGesture

(为了显示目的,我没有让panView完全透明,您应该使用clearColor而不是将alpha设置为0。)

(显然,panView是mainView的子视图,因此当翻译mainView时,panView也会被翻译。)

其次,如果您不想触摸,可以自定义UIPanGestureRecognizer以实施touchesBegan:并举起一个标记(如shouldRecognizeNow并签入move:方法)在特定地点开始时被认出。 (您可能需要检查CGRectContainsPoint:方法。)

我不会详细介绍,这里有一个开始的链接:

https://stackoverflow.com/a/19145354/3227743