-2

我有一些关于使用UIPanGestureRecognizer. 是否可以UIImageView一次移动一个。

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

多谢你们。

4

1 回答 1

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看到这个

于 2013-04-22T09:48:46.273 回答