【问题标题】:iPhone - UIImageView moves when Rotate Animation > 90 degreeiPhone - UIImageView 在旋转动画 > 90 度时移动
【发布时间】:2014-04-21 12:20:03
【问题描述】:

我正在使用此代码来旋转我的图像。

- (IBAction)spin:(id)sender {

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.imgRad.transform = CGAffineTransformMakeRotation(arc4random() % 360 * M_PI / 180);
    [UIView commitAnimations];
}

现在我有两个问题。

第一:如果随机数大于 90,则图像的中心向右上角靠拢。并且它与随机数有关。所以更高的数字=图像的位置更靠近右上角。我该如何解决?我的UIImageView 每次大于 90 时都必须设置一个新中心吗?

秒: 如果数字是例如 300,则图像不会旋转 300 度,它只会旋转 60 度。我该如何解决这个问题?

非常感谢!

【问题讨论】:

    标签: ios objective-c animation uiimageview


    【解决方案1】:
    • 第一个问题可能是使用transform时锚点错误导致的,可以避免使用CAAnimation
    • 第二个问题可以使用NSNumber解决。

    代码:

        CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotation.fromValue = [NSNumber numberWithFloat:0];
        rotation.toValue = [NSNumber numberWithFloat:(arc4random() % 360 * M_PI / 180)];
        rotation.duration = 5.0f;
        rotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        rotation.fillMode = kCAFillModeForwards;
        rotation.removedOnCompletion = NO;
    
        [self.imgRad.layer addAnimation:rotation forKey:@"rotatationRandom"];
    

    【讨论】:

    • 太棒了!谢谢你。完美工作:-)
    • 也许还有一个问题:我如何设置动画从最后一个状态开始(如方法“setAnimationBeginsFromCurrentState”),因为如果我再次启动动画,我希望动画从它停止的位置
    • 只保留上一个轮换的toValue,并将其用作当前轮换的fromValue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多