将UIPanGestureRecognizer限制为特定区域

时间:2013-03-30 07:32:42

标签: uipangesturerecognizer

在我看来,我在右侧部分3 UISlider和左侧部分,用户可以使用UIPanGestureRecognizer滑动菜单。有时,当用户使用滑块时,它也会拖动视图。我不希望这种情况发生。我在panLayer方法中有这个代码:

- (IBAction)panLayer:(UIPanGestureRecognizer *)pan {

if (pan.state == UIGestureRecognizerStateChanged) {
    CGPoint point= [pan translationInView:self.topLayer];
    CGRect frame = self.topLayer.frame;
    frame.origin.x = MIN(258.0f, self.layerPosition + point.x);
    if (frame.origin.x < 0) frame.origin.x = 0;
    self.topLayer.frame = frame;
 }
if (pan.state == UIGestureRecognizerStateEnded) {
    if (self.topLayer.frame.origin.x <=160) {
        [self animateLayerToPoint:0];
    }
    else {
        [self animateLayerToPoint:VIEW_HIDDEN];
    }
 }


}

我想用x位置创建一个if语句。像这样:

if (firstTouch.x < 150 && firstTouch.x > 0) {
   //Add my previous code
}

如何在视图中访问firstTouch,如果它在此区域,请运行UIPanGestureRecognizer?

由于

1 个答案:

答案 0 :(得分:1)

这可以通过采用UIGestureRecognizerDelegate并实现以下方法轻松实现:

-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
    return touch.view != myUISlider;
}