ios - UIPanGestureRecognizer 干扰滚动

标签 ios objective-c uiscrollview uipangesturerecognizer

我有一个带有 3 个 View Controller 的 ScrollView ,我想在它们之间滑动。但是,我的一个 View Controller 有一个 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 的高度。因此,为此我只需要上/下平移手势,而 ScrollView 只需要左/右平移手势。

有人知道我该怎么做吗?

最佳答案

您的 ScrollView 有一个名为 panGestureRecognizer 的属性。您可以为自己的 UIPanGestureRecognizer 创建一个委托(delegate),并实现此方法:

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

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

关于ios - UIPanGestureRecognizer 干扰滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19847329/

相关文章:

javascript - iOS 原生应用,与 Cordova 2.9.0 集成

ios - 如何在 Storyboard 导航 Controller 中替换/自定义后退按钮图像

ios - 设置 UITableViewHeaderFooterView 边距值(滚动时)

ios - 使用 Storyboard 和 Autolayout 旋转到横向后,UIScrollView 中的 UIButtons 不可触摸?

ios - 如果显示在窗口上,MBProgressHUD 无法更改文本

ios - 验证 objective-c 中同一文本字段中的电话号码和电子邮件

ios - NSTextContainer exclusionPaths 卡住应用程序并在 iOS 7.1 上使用 99% 的 CPU - 解决方法?

objective-c - 如何在 applicationWillFinishLaunching 委托(delegate)中终止 cocoa 应用程序

初始化时 alloc 返回的 Objective-C 类与错误的类混淆

ios - 从 UIScrollView Swift 禁用水平滚动