ImageView panGesture 取消 UISCrollVIew 滚动功能

Posted

技术标签:

【中文标题】ImageView panGesture 取消 UISCrollVIew 滚动功能【英文标题】:ImageView panGesture cancelling UISCrollVIew scroll feature 【发布时间】:2013-03-18 08:21:39 【问题描述】:

我有一个滚动视图,其中有多个图像视图,我在这些视图上应用平移手势来拖动图像视图并添加到另一个视图。问题是当我尝试滚动滚动视图时,首先识别平移手势并调用平移操作而不是滚动视图滚动。 这是我的平移代码

-(void)move:(UIPanGestureRecognizer *)sender


    [[baseView superview]  bringSubviewToFront:baseView]; //sender.view];

    [hatScroll setScrollEnabled:NO];

    // We pass in the gesture to a method that will help us align our touches so that the pan and pinch will seems to originate between the fingers instead of other points or center point of the UIView
    [self adjustAnchorPointForGestureRecognizer:sender];

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

        if ([[[sender view] superview] superview] == itemView || [[[sender view] superview] superview] == itemView_iphone5) // adds the imageview in the item scroll
        
            int tag = [[sender view] tag] / 100;
            UIScrollView *tempScroll;

            if (window.bounds.size.height != 568)
                tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
            else
                tempScroll = (UIScrollView *)[itemView_iphone5 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];
            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.cancelsTouchesInView = NO;
            panRecognizer.delaysTouchesBegan = NO;
            panRecognizer.delaysTouchesEnded = NO;
            [panRecognizer setDelegate:self];
            [tempImageView addGestureRecognizer:panRecognizer];
        
    

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

        if (window.bounds.size.height != 568)
        
            if ([sender view].frame.size.width == 35 && [sender view].frame.size.height == 35)
                [[sender view] sizeToFit];
        
        else
        
            if ([sender view].frame.size.width == 45 && [sender view].frame.size.height == 45)
                [[sender view] sizeToFit];
        


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

        [baseView addSubview:[sender view]];
    

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
    

        [hatScroll setScrollEnabled:YES];

        [self.view insertSubview:baseView atIndex:2];



        CGPoint newPoint;
        if (window.bounds.size.height != 568)
            newPoint = [[[sender view] superview] convertPoint:newTranslatedPoint toView:itemView];
        else
            newPoint = [[[sender view] superview] convertPoint:newTranslatedPoint toView:itemView_iphone5];

        if (newPoint.y < 0)
        

            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)
        

            int tag = [[sender view] tag] / 100;

            UIScrollView *tempScroll;

            if (window.bounds.size.height != 568)
                tempScroll = (UIScrollView *)[itemView viewWithTag:-tag];
            else
                tempScroll = (UIScrollView *)[itemView_iphone5 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]];

        
    

有什么想法吗?我怎么能得到这个。随意在提到的代码询问查询。

【问题讨论】:

如果你想将手势传递给添加了 uiimageview 的滚动视图。简单设置 [tempImageView setUserInteraction:NO]; 那么提供给 imgaeview 的 pangesture 将如何工作 与 Xcode 无关,xcode 只是 IDE -- 不是 sdk,也不是 api 也不是语言 【参考方案1】:

尝试使用长按手势从滚动视图中拖动特定项目

【讨论】:

以上是关于ImageView panGesture 取消 UISCrollVIew 滚动功能的主要内容,如果未能解决你的问题,请参考以下文章

如何在 panGesture 之后修复新的 textField 位置?

按下取消按钮时如何重设标记视图的位置?

取消当前的长手势来拖动视图?

如何使用 panGesture 从 firstViewController 的顶部移动 secondViewController?

将图像从 UIScrollView 移动到 UIView - Swift 4 PanGesture

如何以编程方式快速发送 pangesture