缩放、旋转和拖动 UIImageView

发布于 2024-11-01 09:15:00 字数 4649 浏览 3 评论 0原文

-(void)scale:(id)sender {

[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

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

    lastScale = 1.0;
    return;
}

CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

-(void)rotate:(id)sender {

[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

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

    lastRotation = 0.0;
    return;
}

CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

-(void)move:(id)sender {

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

[self.view 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 + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
    CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

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

        if(finalX < 0) {                                finalX = 0;             }                       else if(finalX > 768) {

            finalX = 768;
        }

        if(finalY < 0) {                                finalY = 0;             }                       else if(finalY > 1024) {

            finalY = 1024;
        }
    }

    else {

        if(finalX < 0) {                                finalX = 0;             }                       else if(finalX > 1024) {

            finalX = 768;
        }

        if(finalY < 0) {                                finalY = 0;             }                       else if(finalY > 768) {

            finalY = 1024;
        }
    }

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

-(void)tapped:(id)sender {

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer      shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

这是我的代码,我想要移动的 UIImageView 称为“Birdie”。我知道我必须创建一个 -(void)Birdie UIImageView 识别器,但我使用了以下内容,但不起作用。

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

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

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

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[Birdie addGestureRecognizer:tapRecognizer];

[self.view addSubview:Birdie];
}

非常感谢!

-(void)scale:(id)sender {

[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

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

    lastScale = 1.0;
    return;
}

CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];
}

-(void)rotate:(id)sender {

[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

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

    lastRotation = 0.0;
    return;
}

CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}

-(void)move:(id)sender {

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

[self.view 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 + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
    CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

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

        if(finalX < 0) {                                finalX = 0;             }                       else if(finalX > 768) {

            finalX = 768;
        }

        if(finalY < 0) {                                finalY = 0;             }                       else if(finalY > 1024) {

            finalY = 1024;
        }
    }

    else {

        if(finalX < 0) {                                finalX = 0;             }                       else if(finalX > 1024) {

            finalX = 768;
        }

        if(finalY < 0) {                                finalY = 0;             }                       else if(finalY > 768) {

            finalY = 1024;
        }
    }

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

-(void)tapped:(id)sender {

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer      shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}

This is my code and the UIImageView I want to move is called "Birdie". I know I have to create a -(void)Birdie UIImageView recognizer but I used the following which didn't worked.

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

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

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

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[Birdie addGestureRecognizer:tapRecognizer];

[self.view addSubview:Birdie];
}

Thanks alot in advanced!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-11-08 09:15:00

您需要在界面中放置一些值,例如:

//Zoom
CGFloat lastScaleFactor;

//Movement
CGPoint startLocation;
float distance;

//Rotation
CGFloat valore;

然后:

- (void)zoomAction:(UIGestureRecognizer *)sender {

    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    if (factor > 1) {

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1), lastScaleFactor + (factor-1));

    } else {

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor);

    }

    //Save the new "scale" factor
    if (sender.state == UIGestureRecognizerStateEnded) { 
        if (factor > 1) {
            lastScaleFactor += (factor-1); 
        } else {
            lastScaleFactor *= factor;
        }
    }
}
- (void)panAction:(UIPanGestureRecognizer *)gesture {
    if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) {

        CGPoint location = [gesture locationInView:self.view];

        [self.yourImage setCenter:location];
    }
}

- (void)rotationAction:(UIRotationGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
         [gesture setRotation:valore * 10];
    }
    if (gesture.state == UIGestureRecognizerStateChanged) {
        valore = gesture.rotation / 10;
        if (valore > 1.0) valore = 1.0;
        if (valore < 0.2) valore = 0.2;

        self.yourImage.alpha = valore;
    }
    NSLog(@"rotazione %f", gesture.rotation / 10);
}

在这种情况下,我使用旋转来更改 alpha,但您可以执行您需要的操作

You need to put some values in the in the interface, something like:

//Zoom
CGFloat lastScaleFactor;

//Movement
CGPoint startLocation;
float distance;

//Rotation
CGFloat valore;

Then:

- (void)zoomAction:(UIGestureRecognizer *)sender {

    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    if (factor > 1) {

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1), lastScaleFactor + (factor-1));

    } else {

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor);

    }

    //Save the new "scale" factor
    if (sender.state == UIGestureRecognizerStateEnded) { 
        if (factor > 1) {
            lastScaleFactor += (factor-1); 
        } else {
            lastScaleFactor *= factor;
        }
    }
}
- (void)panAction:(UIPanGestureRecognizer *)gesture {
    if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) {

        CGPoint location = [gesture locationInView:self.view];

        [self.yourImage setCenter:location];
    }
}

- (void)rotationAction:(UIRotationGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
         [gesture setRotation:valore * 10];
    }
    if (gesture.state == UIGestureRecognizerStateChanged) {
        valore = gesture.rotation / 10;
        if (valore > 1.0) valore = 1.0;
        if (valore < 0.2) valore = 0.2;

        self.yourImage.alpha = valore;
    }
    NSLog(@"rotazione %f", gesture.rotation / 10);
}

In this case I used rotation to change the alpha, but you can do what you need

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文