0

我想旋转 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。我该怎么做呢?

4

2 回答 2

0

为什么不使用标准UIView动画?如果您真的需要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:];

于 2013-08-22T18:46:09.383 回答
-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"];
}
于 2013-08-22T16:50:47.660 回答