2

我想实现对图像视图的平移手势功能,这个图像视图是滚动视图的子视图。问题是当我尝试滚动滚动视图时,图像视图的平移手势被识别,并且滚动视图不滚动。请帮我解决一下这个。

这是将 pan 初始化为 uiimageview 的代码:

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
            [panRecognizer setMinimumNumberOfTouches:1];
            [panRecognizer setMaximumNumberOfTouches:1];
            [panRecognizer setDelegate:self];
            [[[scrollView subviews] objectAtIndex:i] addGestureRecognizer:panRecognizer];

imageview 将用户设置为“YES”

以及为此采取的行动

-(void)move:(UIPanGestureRecognizer *)sender
{
    [[[[[sender view] superview] superview] superview]  bringSubviewToFront:sender.view];


    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {

        if ([[[sender view] superview] superview] == itemView) // adds the imageview in the item scroll
        {
            int tag = [[sender view] tag] / 100;
            UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
            CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]);

            CGAffineTransform transform = temp.transform;
            UIImageView *tempImageView =  [[UIImageView alloc] initWithImage: [(UIImageView *) [sender view] image]];
            tempImageView.tag = [[sender view] tag];
            //NSLog(@"[sender view] superview] : %@", [[sender view] superview]);
            tempImageView.frame = frame;
            [tempImageView setTransform:transform];
            [tempImageView setFrame:frame];
            [tempImageView setUserInteractionEnabled:YES];
            [tempScroll addSubview:tempImageView];

            UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
            [panRecognizer setMinimumNumberOfTouches:1];
            [panRecognizer setMaximumNumberOfTouches:1];
            [panRecognizer setDelegate:self];
            [tempImageView addGestureRecognizer:panRecognizer];

            UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            [singleTapGestureRecognizer setNumberOfTapsRequired:1];
            [tempImageView addGestureRecognizer:singleTapGestureRecognizer];
        }
    }

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged)
    {
        CGPoint translatedPoint = [sender locationInView:[[sender view] superview]] ;
        newTranslatedPoint = [[[sender view] superview] convertPoint:translatedPoint toView:self.view];

        if ([sender view].frame.size.width == 35 && [sender view].frame.size.height == 35)
            [[sender view] sizeToFit];

        [sender view].center =  CGPointMake(newTranslatedPoint.x, newTranslatedPoint.y); //startFrame;

        [self.view addSubview:[sender view]];
    }

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
    {
        CGPoint newPoint = [[[sender view] superview] convertPoint:newTranslatedPoint toView:itemView];
        if (newPoint.y < 0)// || !viewUp)
        {
            //NSLog(@" baseView");
            [baseView addSubview:[sender view]];

            UITapGestureRecognizer *doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
            [doubleTapGestureRecognizer setNumberOfTapsRequired:2];
            [[sender view] addGestureRecognizer:doubleTapGestureRecognizer];

            UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
            [pinchRecognizer setDelegate:self];
            [[sender view] addGestureRecognizer:pinchRecognizer];

            UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
            [rotationRecognizer setDelegate:self];
            [[sender view] addGestureRecognizer:rotationRecognizer];
        }

        else if (newPoint.y > 0)// && viewUp )
        {
            //NSLog(@" itemView");
            int tag = [[sender view] tag] / 100;
            UIScrollView *tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
            CGRect frame = CGRectFromString([itemFrameDict objectForKey:[NSString stringWithFormat:@"%i",[[sender view] tag]]]);

            CGAffineTransform transform = temp.transform;
            [[sender view] setTransform:transform];
            [[sender view] setFrame:frame];
            [tempScroll addSubview:[sender view]];
        }
    }
}
4

1 回答 1

3

我不确定我是否理解您通过在 aUIPanGestureRecognizer上使用UIImageView已经在 a 中的a 来准确实现的目标UIScrollView。但从你的描述来看,听起来你想要UIScrollView做这件事并且UIPanGestureRecognizer不阻止它。我相信你需要为你实现这个委托方法UIPanGestureRecognizer

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

请阅读关于允许同时手势识别的UIGestureRecognizerDelegate 协议参考。根据文档:

默认实现返回 NO——不能同时识别两个手势。

/* 编辑 */

来自UIPanGestureRecognizer - 仅垂直或水平: 仅实现一个方向UIPanGestureRecognizer。这是垂直平移:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGPoint translation = [panGestureRecognizer translationInView:someView];
    return fabs(translation.y) > fabs(translation.x);
}
于 2013-04-12T06:36:36.697 回答