UIImageView的平移手势会干扰滚动

时间:2013-04-12 04:57:50

标签: xcode uiscrollview uiimageview scroll uipangesturerecognizer

我想将平移手势的功能实现到imageview,此imageview是滚动视图的子视图。问题是当我尝试滚动滚动视图时,图像视图的平移手势被识别,并且滚动视图不会滚动。 请帮帮我。

这是将平移初始化为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将用户设置为“是”

及其行动

-(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]];
        }
    }
}

1 个答案:

答案 0 :(得分:3)

我不确定通过UIPanGestureRecognizer UIImageView UIScrollView使用UIScrollView来确切了解您正在努力实现的目标。但是根据您的描述,听起来您希望UIPanGestureRecognizer执行此操作并且UIPanGestureRecognizer不会阻止它。我相信您需要为- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }

实施此委托方法
UIPanGestureRecognizer

请阅读UIGestureRecognizerDelegate Protocol reference关于允许同时进行手势识别的问题。根据文件:

  

默认实现返回NO - 没有两个手势可以   同时认可。

/ *编辑* /

来自UIPanGestureRecognizer - Only vertical or horizontal: 只为- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer { CGPoint translation = [panGestureRecognizer translationInView:someView]; return fabs(translation.y) > fabs(translation.x); } 实现一个方向。这是一个垂直平底锅:

{{1}}