How do I achieve continuous rotation of an NSImage inside NSImageView?

前端 未结 2 830
暖寄归人
暖寄归人 2021-02-01 10:17

FUTURE VIEWERS:

I have managed to finish this rotation animation and code with description can be found on tho question. NSImage rotation in NSView is not working

<
2条回答
  •  执念已碎
    2021-02-01 11:09

    Update:

    I've recently learned of another way to handle rotations of greater than 180 degrees, or continuous rotations.

    There is a special object called a CAValueFunction that lets you apply a change to your layer's transform using an arbitrary value, including values that specify multiple full rotations.

    You create a CABasicAnimation of your layer's transform property, but then instead of providing a transform, the value you supply is an NSNumber that gives the new rotation angle. If you provide a new angle like 20pi, your layer will rotate 10 full rotations (2pi/rotation). The code looks like this:

    //Create a CABasicAnimation object to manage our rotation.
    CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:@"transform"];
    
    rotation.duration = 10.0;
    CGFLOAT angle = 20*M_PI;
    
    //Set the ending value of the rotation to the new angle.
    rotation.toValue = @(angle);
    
    //Have the rotation use linear timing.
    rotation.timingFunction = 
      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    /*
     This is the magic bit. We add a CAValueFunction that tells the CAAnimation we are 
     modifying the transform's rotation around the Z axis.
     Without this, we would supply a transform as the fromValue and toValue, and 
     for rotations > a half-turn, we could not control the rotation direction.
    
     By using a value function, we can specify arbitrary rotation amounts and 
     directions and even rotations greater than 360 degrees.
    */
    
    rotation.valueFunction = 
      [CAValueFunction functionWithName: kCAValueFunctionRotateZ];
    
    
    
    /*
     Set the layer's transform to it's final state before submitting the animation, so 
     it is in it's final state once the animation completes.
    */
    imageViewToAnimate.layer.transform = 
      CATransform3DRotate(imageViewToAnimate.layer.transform, angle, 0, 0, 1.0);
    
    [imageViewToAnimate.layer addAnimation:rotation forKey:@"transform.rotation.z"];
    

    (I Extracted the code above from a working example application, and took out some things that weren't directly related to the subject. You can see this code in use in the project KeyframeViewAnimations (link) on github. The code that does the rotation is in a method called `handleRotate'

提交回复
热议问题