How to let a view rotate forever?

前端 未结 3 1285
执念已碎
执念已碎 2021-01-13 06:49

Is there a way to let a view rotate forever, with an specified speed? I need that for an indicator kind of thing. I know there is this weird Lxxxxx00ff constant (don\'t reme

3条回答
  •  萌比男神i
    2021-01-13 07:55

    You can use HUGE_VAL for floating value (if I remember correctly, repeatCount property for animation is a float).

    To setup animation you can create CAAnimation object using +animationWithKeyPath: method:

    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
    animation.duration = 3.0f;
    animation.repeatCount = HUGE_VAL;
    [rotView.layer addAnimation:animation forKey:@"MyAnimation"];
    

    If I remember correctly creating this kind of rotation using just UIView animations is impossible because rotations on 360 degrees (2*M_PI radians) are optimized to no rotation at all.


    Edit: Added a Swift version.

        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.fromValue = NSNumber(value: 0.0)
        animation.toValue = NSNumber(value: 2*Double.pi)
        animation.duration = 3.0
        animation.repeatCount = Float.greatestFiniteMagnitude
        rotView.layer.add(animation, forKey: "MyAnimation")
    

提交回复
热议问题