rotate a UIView around its center but several times

前端 未结 4 1404
忘了有多久
忘了有多久 2020-12-01 06:43

I\'m trying to rotate some UIView around its center, so the simple code goes something like (in pseudocode):

[UIView beginAnimations:@\"crazyRo         


        
相关标签:
4条回答
  • 2020-12-01 07:18

    As Brad Larson indicated, you can do this with a CAKeyframeAnimation. For instance,

    CAKeyframeAnimation *rotationAnimation;
    rotationAnimation = 
       [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    rotationAnimation.values = [NSArray arrayWithObjects:
                                [NSNumber numberWithFloat:0.0 * M_PI], 
                                [NSNumber numberWithFloat:0.75 * M_PI], 
                                [NSNumber numberWithFloat:1.5 * M_PI], 
                                [NSNumber numberWithFloat:2.0 * M_PI], nil]; 
    rotationAnimation.calculationMode = kCAAnimationPaced;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.timingFunction = 
       [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotationAnimation.duration = 10.0;
    
    CALayer *layer = [viewToSpin layer];
    [layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    

    You can control the duration of the total animation with the rotationAnimation.duration property, and the acceleration and deceleration (and calculation of steps in between) with the rotationAnimation.timingFunction property.

    0 讨论(0)
  • 2020-12-01 07:29

    Getting a continuous spinning effect is a little tricky, but I describe a means to do it here. Yes, Core Animation seems to optimize transforms to the closest ending position within the unit circle. The method I describe there chains a few half-rotation animations together to make full rotations, although you do notice a slight stutter in the handoff from one animation to the next.

    Perhaps a CAKeyframeAnimation constructed with these half-rotation values would be the right way to go. Then you could also control acceleration and deceleration.

    0 讨论(0)
  • 2020-12-01 07:30
    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
    animation.duration = 8.0f;
    animation.repeatCount = INFINITY;
    [self.myView.layer addAnimation:animation forKey:@"SpinAnimation"];
    
    0 讨论(0)
  • 2020-12-01 07:37

    You can use the following animation on your UIView's layer property. I've tested it.

    Objective-C

    UIView *viewToSpin = ...;    
    CABasicAnimation* spinAnimation = [CABasicAnimation
                                      animationWithKeyPath:@"transform.rotation"];
    spinAnimation.toValue = [NSNumber numberWithFloat:5*2*M_PI];
    [viewToSpin.layer addAnimation:spinAnimation forKey:@"spinAnimation"];
    

    Swift 5.0

    let viewToSpin = UIView() // However you have initialized your view
    let spinAnimation = CABasicAnimation.init(keyPath: "transform.rotation")
    spinAnimation.toValue = NSNumber(value: 5.0 * 2.0 * Float.pi)
    viewToSpin.layer.add(spinAnimation, forKey: "spinAnimation")
    
    0 讨论(0)
提交回复
热议问题