Ios 干扰滚动的UIPanGestureRecognizer

Ios 干扰滚动的UIPanGestureRecognizer,ios,objective-c,uiscrollview,uipangesturerecognizer,Ios,Objective C,Uiscrollview,Uipangesturerecognizer,我有一个带有3个视图控制器的滚动视图,我想在它们之间滑动。但是,我的一个视图控制器有一个UIPangestureRecognitor,这会使scrollview停止工作。这是pan的代码: - (void)pan:(UIPanGestureRecognizer *)aPan; // When pan guesture is recognised { CGPoint location = [aPan locationInView:self.view]; // Location of finger

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

- (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];
}
}
-(void)pan:(UIPangestureRecognitor*)aPan;//当泛来宾被认可时
{
CGPoint location=[aPan locationInView:self.view];//手指在屏幕上的位置
CGRect secondRect=CGRectMake(210.0,45.0,70.0,325.0);//最大条形面积的矩形
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位于“second”矩形内
CGPoint currentPoint=[aPan locationInView:self.view];
currentPoint.y-=80;//确保动画不会超出条形图的矩形
如果(当前点y<0){
电流点y=0;
}
如果(currentPoint.y>239)发生其他情况{
电流点y=239;
}
currentPoint.y=239.0—currentPoint.y;
CGFloat pointy=currentPoint.y-fmod(currentPoint.y,4.0);
[UIView animateWithDuration:0.01f//设置条形图的动画,使其随着手指的上下移动而上升
动画:^{
CGRect oldFrame=secondBar.frame;
secondBar.frame=CGRectMake(oldFrame.origin.x,(oldFrame.origin.y-(pointy-secondBar.frame.size.height)),oldFrame.size.width,(pointy));
}];
CGFloat结果=secondBar.frame.size.height-fmod(secondBar.frame.size.height,4.0);
secondInt=(result/4.0);//使用新时间更新标签
self->secondLabel.text=[nsstringwithformat:@“%02d”,secondInt];
}
}
基本上,此代码允许用户在屏幕上上下拖动手指,并更改uiview的高度。因此,我只需要向上/向下平移手势,滚动视图只需要左/右平移手势


有人知道我该怎么做吗?

uipangesturecognitioner
包含一个委托方法
-(BOOL)手势识别器:(uigesturecognitioner*)手势识别器应该接收触摸:(UITouch*)触摸
,您可以返回
,以允许/阻止平移手势接收触摸

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

- (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
    }
}
-(BOOL)手势识别器:(UIGestureRecognitor*)手势识别器应接受触摸:(UITouch*)触摸
{
CGPoint velocity=[GestureRecognitor VelocityView:yourView];
如果(速度x>0)
{
NSLog(@“手势向右”);//返回否
}
否则如果(速度x<0)
{
NSLog(@“手势向左”);//返回否
}
如果(速度y>0)
{
NSLog(@“手势下降”);//返回是
}
否则如果(速度y<0)
{
NSLog(@“手势向上”);//返回是
}
}

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

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

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

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

你试过这个吗?我认为它不起作用,因为当决定允许手势识别器接收触摸时,它仍然处于零速度。我认为你是对的。我希望在回答这个问题之前我能进一步测试一下,哈哈