使用UIPanGestureRecognizer一次移动一个UIImageView

时间:2013-04-22 09:42:13

标签: objective-c ios5 ios6 uiimageview uipangesturerecognizer

我对使用UIPanGestureRecognizer有一些疑问。是否可以一次移动UIImageView一个。

流程是这样的。我有2张图片。图片A和B.如果您选择image A,则image B不能是moved

谢谢你们。

1 个答案:

答案 0 :(得分:0)

您只能为特定的UIImageView对象设置UIPanGestureRecognizer。

    //set UIPanGestureRecognizer for first image
    UIPanGestureRecognizer *panRecognizer1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage1:)];
    [panRecognizer1 setMinimumNumberOfTouches:1];
    [panRecognizer1 setMaximumNumberOfTouches:1];
    [panRecognizer1 setDelegate:self];
    [img1 addGestureRecognizer:panRecognizer1];

    //set UIPanGestureRecognizer for second image
    UIPanGestureRecognizer *panRecognizer2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage2:)];
    [panRecognizer2 setMinimumNumberOfTouches:1];
    [panRecognizer2 setMaximumNumberOfTouches:1];
    [panRecognizer2 setDelegate:self];
    [img1 addGestureRecognizer:panRecognizer2];

并编写两种图像移动方法

    -(void)moveImage1:(id)sender {
            //coding for moving first image
            [[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
            [viewGesture bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
            CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

            if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
                    firstX = [[sender view] center].x;
                    firstY = [[sender view] center].y;
            }

            translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
            [[sender view] setCenter:translatedPoint];

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


                    CGFloat finalX = translatedPoint.x + (.04*[(UIPanGestureRecognizer*)sender velocityInView:viewGesture].x);
                    CGFloat finalY = translatedPoint.y + (.04*[(UIPanGestureRecognizer*)sender velocityInView:viewGesture].y);

                    if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {

                            if(finalX < -5000) {              finalX = 0;       }
                            else if(finalX > 5000) {          finalX = 320;     }

                            if(finalY < -5000) {         finalY = 0;             }
                            else if(finalY > 5000) {     finalY = 460;           }
                    }
                    else {
                            if(finalX < -5000) {   finalX = 0;    }
                            else if(finalX > 5000) {  finalX = 320;}

                            if(finalY < -5000) {         finalY = 0;             }
                            else if(finalY > 5000) {     finalY = 460;        }
                    }

                    [UIView beginAnimations:nil context:NULL];
                    [UIView setAnimationDuration:.35];
                    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                    [[sender view] setCenter:CGPointMake(finalX, finalY)];
                    [UIView commitAnimations];
            }
    }

    -(void)moveImage2:(id)sender {
            //coding for moving second image
    }

并且还可以使用uicollectionview

查看this