How to rotate an object around a arbitrary point?

后端 未结 4 1322
醉酒成梦
醉酒成梦 2020-12-05 08:32

I want to rotate an UILabel around an arbitrary point in a circular manner, not a straight line. This is my code.The final point is perfect but it goes through a straight li

4条回答
  •  悲&欢浪女
    2020-12-05 09:14

    I decided to post my solution as an answer. It works fine accept it doesn't have the old solutions's curve animations (UIViewAnimationCurveEaseOut), but I can sort that out.

    #define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)
    
    - (void)rotateText:(UILabel *)label duration:(NSTimeInterval)duration degrees:(CGFloat)degrees {
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddArc(path,nil, 160, 236, 100, DEGREES_TO_RADIANS(0), DEGREES_TO_RADIANS(degrees), YES);
    
        CAKeyframeAnimation *theAnimation;
    
        // animation object for the key path
        theAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        theAnimation.path=path;
        CGPathRelease(path);
    
        // set the animation properties
        theAnimation.duration=duration;
        theAnimation.removedOnCompletion = NO; 
        theAnimation.autoreverses = NO;
        theAnimation.rotationMode = kCAAnimationRotateAutoReverse;
        theAnimation.fillMode = kCAFillModeForwards;
    
        [label.layer addAnimation:theAnimation forKey:@"position"]; 
    }
    

提交回复
热议问题