UIPanGestureRecognizer 干扰滚动

Posted

技术标签:

【中文标题】UIPanGestureRecognizer 干扰滚动【英文标题】:UIPanGestureRecognizer interfering with scroll 【发布时间】:2013-11-07 21:46:42 【问题描述】:

我有一个带有 3 个视图控制器的滚动视图,我想在它们之间滑动。但是,我的一个视图控制器有一个 UIPanGestureRecognizer,这会阻止滚动视图工作。这是锅的代码:

- (void)pan:(UIPanGestureRecognizer *)aPan; // When pan guesture is recognised

CGPoint location = [aPan locationInView:self.view]; // Location of finger on screen
CGRect secondRect = CGRectMake(210.0, 45.0, 70.0, 325.0); // Rectangles of maximimum bar area
CGRect minuteRect = CGRectMake(125.0, 45.0, 70.0, 325.0);
CGRect hourRect = CGRectMake(41.0, 45.0, 70.0, 325.0);

if (CGRectContainsPoint(secondRect, location))  // If finger is inside the 'second' rectangle

    CGPoint currentPoint = [aPan locationInView:self.view];

    currentPoint.y -= 80;  // Make sure animation doesn't go outside the bars' rectangle

    if (currentPoint.y < 0) 
    currentPoint.y = 0;
    
    else if (currentPoint.y > 239) 
    currentPoint.y = 239;
    

    currentPoint.y = 239.0 - currentPoint.y;

    CGFloat pointy = currentPoint.y - fmod(currentPoint.y, 4.0);

    [UIView animateWithDuration:0.01f  // Animate the bars to rise as the finger moves up and down
                     animations:^
                         CGRect oldFrame = secondBar.frame;
                         secondBar.frame = CGRectMake(oldFrame.origin.x, (oldFrame.origin.y - (pointy - secondBar.frame.size.height)), oldFrame.size.width, (pointy));
                     ];

    CGFloat result = secondBar.frame.size.height - fmod(secondBar.frame.size.height, 4.0);

    secondInt = (result / 4.0); // Update labels with new time

    self->secondLabel.text = [NSString stringWithFormat:@"%02d", secondInt];


基本上,此代码允许用户在屏幕上上下拖动手指,并更改 uiview 的高度。因此,我只需要上/下平移手势和滚动视图的左/右平移手势。

有人知道我该怎么做吗?

【问题讨论】:

【参考方案1】:

您的滚动视图有一个名为 panGestureRecognizer 的属性。您可以为自己的 UIPanGestureRecognizer 创建一个委托,并实现此方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 

    if(otherGestureRecognizer == myScrollView.panGestureRecognizer) 
         return YES;
     else 
         return NO;
    

【讨论】:

【参考方案2】:

UIPanGestureRecognizer 包含一个委托方法- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch,您可以返回YESNO 以允许/防止平移手势接收触摸。

在该方法中,您可以编写如下内容:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

    CGPoint velocity = [gestureRecognizer velocityInView:yourView];

    if(velocity.x > 0)
    
        NSLog(@"gesture went right"); // return NO
    
    else if (velocity.x < 0)
    
        NSLog(@"gesture went left"); // return NO
    

    if (velocity.y > 0)
    
        NSLog(@"gesture went down"); // return YES
    
    else if (velocity.y < 0)
    
        NSLog(@"gesture went up"); // return YES
    

诚然,这可能不是最干净的实现。你正在为混合手势留下一扇敞开的大门。因此,请务必注意这一点。

【讨论】:

你试过了吗?我不认为它会起作用,因为当决定允许手势识别器接收触摸时,手势识别器仍将处于零速度。 我认为你是对的。我希望在回答之前我已经对此进行了进一步的测试哈哈

以上是关于UIPanGestureRecognizer 干扰滚动的主要内容,如果未能解决你的问题,请参考以下文章

UIPanGestureRecognizer 碰撞

如何在父视图中限制 UIPanGestureRecognizer

未调用 iOS 13 UIPanGestureRecognizer 选择器

Swift:获取 UIPanGestureRecognizer 的目标

当对象移动到某个帧时如何停止 UIPanGestureRecognizer

在父 UIView 中移动 UIView (UIPanGestureRecognizer)