用动画旋转图像?

时间:2014-04-01 19:33:43

标签: ios xcode animation

我想将图像旋转90度。一切正常。但动画效果不顺畅?

轮换代码

- (void)rotatePhoto {
    UIImage *rotatedImage;

    if (finalImage.imageOrientation == UIImageOrientationRight)
        rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                              scale: 1.0
                                        orientation: UIImageOrientationDown];
    else if (finalImage.imageOrientation == UIImageOrientationDown)
        rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                              scale: 1.0
                                        orientation: UIImageOrientationLeft];
    else if (finalImage.imageOrientation == UIImageOrientationLeft)
        rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                              scale: 1.0
                                        orientation: UIImageOrientationUp];
    else
        rotatedImage = [[UIImage alloc] initWithCGImage: finalImage.CGImage
                                                     scale: 1.0
                                               orientation: UIImageOrientationRight];

  [self updateImageViewAnimated:rotatedImage]
}

我尝试过类似的东西,但不顺利。它旋转但我无法理解

 - (void) updateImageViewAnimated:(UIImage *)image
{

    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI /2];
    rotationAnimation.duration = 0.7;
    rotationAnimation.cumulative = YES;
    [_finalImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

    [UIView transitionWithView:_finalImageView
                      duration:0.7                           options:UIViewAnimationOptionTransitionNone
                    animations:^{
                        _finalImageView.image = image;
                    } completion:NULL];

}

1 个答案:

答案 0 :(得分:0)

你误解了UIImageOrientationRight等等。这些是照片的固有质量(例如,因为照片是在相机旋转的情况下拍摄的)。它们与旋转图像无关。要使用动画旋转图像,只需旋转图像视图 - 即应用旋转变换。不要使用transitionWithView:;只需旋转(animateWithDuration:...)。变换将告诉您当前旋转的内容,然后您可以根据需要稍后旋转。