让 UIPanGesture 和 UISwipeGesture 朝着同一个方向一起工作

Posted

技术标签:

【中文标题】让 UIPanGesture 和 UISwipeGesture 朝着同一个方向一起工作【英文标题】:Getting UIPanGesture and UISwipeGesture working together in the same direction 【发布时间】:2014-05-26 20:02:07 【问题描述】:

我已经设置了我的UIView,以便在视图向上平移时调用一个方法,并在视图向上滑动时调用其他方法。我的平底锅工作正常,如果我的 velocity.y 超过某个限制,我可以让我的平底锅保持禁用状态,但是当我的平底锅出现故障时,我永远无法让滑动动作起作用。我尝试过使用委托方法,但运气不佳。看过这个解决方案但没有运气:https://***.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);
    

【问题讨论】:

你应该(a)在你实例化它时指定滑动手势的方向(它让你不必担心平移的速度); (b) 使平底锅要求滑动失败,而不是相反。你甚至可能不再需要那个委托方法了…… 【参考方案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");

【讨论】:

有效!为什么要去掉swipeRec.delegate = selfpanRec.delegate=self 你可以保留这些,但我删除了它们,因为它们对于这个特定目的不是必需的。

以上是关于让 UIPanGesture 和 UISwipeGesture 朝着同一个方向一起工作的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIPanGesture 拖动 tableView/scrollView

Swift 中的 UIScrollView 内的 UIPanGesture

使用 UIPanGesture 时发送到实例的无法识别的选择器

UipanGesture 不适用于 Uiswitch

在 iPhone 中使用 UIPanGesture 旋转 UIView 的框架增加减少

忽略 UIViewController 中的 UIPanGesture