首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIPanGestureRecognizer干扰滚动

UIPanGestureRecognizer干扰滚动
EN

Stack Overflow用户
提问于 2013-11-08 05:46:43
回答 2查看 1.4K关注 0票数 1

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

代码语言:javascript
复制
- (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的高度。因此,我只需要向上/向下的平移手势和滚动视图的左/右平移手势。

有谁知道我该怎么做吗?

EN

回答 2

Stack Overflow用户

发布于 2013-11-08 08:03:48

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

代码语言:javascript
复制
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if(otherGestureRecognizer == myScrollView.panGestureRecognizer) {
         return YES;
    } else {
         return NO;
    }
}
票数 3
EN

Stack Overflow用户

发布于 2013-11-08 06:00:47

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

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

代码语言:javascript
复制
- (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
    }
}

诚然,这可能不是最干净的实现。你为混合的手势留下了一扇敞开的大门。所以一定要注意这一点。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19847329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档