IOS需要旋转图像并使用动画将其保留在那里

时间:2013-08-22 14:57:21

标签: ios animation

我正在寻找旋转UIImageView,我发现了一些有用的代码

- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:    (CGFloat)rotations repeat:(float)repeat;
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}

问题是当动画结束时图像会翻转,我希望旋转它并将其保留在那里。我需要翻转实际图像还是可以翻转ImageView。我该怎么做??

2 个答案:

答案 0 :(得分:0)

为什么不使用标准UIView animation? 如果你真的需要repeatCount,那么我建议你使用旧式:

[UIView beginAnimations]
[UIView setAnimationRepeatCount:repeatCount];
[UIView setAnimationDuration:duration];
yourView.transform = CGAffineTransformMakeRotation(degrees * M_PI);
[UIView commitAnimations];

否则,您可以使用更受欢迎的新风格:

[UIView animateWithDuration:duration animations:^{
    yourView.transform = CGAffineTransformMakeRotation(angle);
} completion:^(BOOL finished) {
    // your completion code here
}]

如果您需要其他选项,例如自动反向,请允许用户互动或重复使用[UIView animateWithDuration:delay:options:animations:completion:];

答案 1 :(得分:-1)

我更喜欢用CoreAnimation覆盖我的所有基础。设置动画前的最终值,设置填充模式,然后设置不删除。

- (void) runSpinAnimationOnView:(UIView*)view duration:(CGFloat)duration rotations:            (CGFloat)rotations repeat:(float)repeat;
{
    NSString * animationKeyPath = @"transform.rotation.z";
    CGFloat finalRotation = M_PI * 2.0f * rotations;
    [view.layer setValue:@(finalRotation) forKey:animationKeyPath];

    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:animationKeyPath];
    rotationAnimation.fromValue = @0.0f;
    rotationAnimation.toValue = @(finalRotation);
    rotationAnimation.fillMode = kCAFillModeBoth;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.duration = duration;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = repeat;

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}