首页 文章

UIPanGestureRecognizer干扰滚动

提问于
浏览
0

我有一个滚动视图,有3个视图控制器,我想在其间滑动 . 但是,我的一个视图控制器有一个UIPanGestureRecognizer,这会阻止scrollview工作 . 这是泛的代码:

- (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的高度 . 因此,我只需要上/下平移手势,只需要滚动视图的左/右平移手势 .

有谁知道我怎么能这样做?

2 回答

  • -1

    您的scrollview有一个名为panGestureRecognizer的属性 . 您可以为自己的UIPanGestureRecognizer创建委托,并实现此方法:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    
        if(otherGestureRecognizer == myScrollView.panGestureRecognizer) {
             return YES;
        } else {
             return NO;
        }
    }
    
  • 3

    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
        }
    }
    

    不可否认,这可能不是最干净的实施 . 你要打开一扇混合手势的门 . 所以,请务必注意 .

相关问题