让Pan Gesture和UISwipeGesture一起朝着同一个方向努力

时间:2014-05-26 20:02:08

标签: ios cocoa-touch ios7

我已将UIView设置为在视图平移时调用方法,并在浏览视图时调用其他方法。我的平底锅工作正常,如果我的速度超过一定限度,我可以保持我的平底锅禁用,但是当我的平底锅发生故障时,我永远无法使用轻扫动作。我尝试过没有太多运气的代理方法。看了解这个解决方案,但没有运气:https://stackoverflow.com/a/21728621/1925859

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    panRec.delegate = self;
    [panedView addGestureRecognizer:panRec];

    UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeRec.delegate = self;
    [panedView addGestureRecognizer:swipeRec];

    [swipeRec requireGestureRecognizerToFail:panRec];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
        if (ABS(velocity.y) > 200)
        {

            NSLog(@"Swipe actiavted");
            return NO;
        }
    }
        return YES;  
}

 - (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"In Swipe");
     if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
    {
        panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
    }
}

1 个答案:

答案 0 :(得分:1)

如果您删除了委托方法,请向滑动添加方向并更改requireGestureRecognizerToFail的接收方然后它将起作用。请注意,平移必须比扫描慢,才能被识别。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [panedView addGestureRecognizer:panRec];

    UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
    [panedView addGestureRecognizer:swipeRec];

    [panRec requireGestureRecognizerToFail:swipeRec];
}


- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"In Swipe");
    if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
    {
        panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
    }
}

- (IBAction)handlePan:(UISwipeGestureRecognizer *)recognizer
{
    NSLog(@"PAN");
}