how to rotate UIIMageVIew with fix point?

后端 未结 3 568
时光取名叫无心
时光取名叫无心 2021-01-15 11:52

I want to rotate image on angle.But i wnt to rotate image with fix point.How can i set this fix point?

3条回答
  •  死守一世寂寞
    2021-01-15 12:21

    If you create a project using the default "View-based Application" in Xcode, just add this code to your -viewDidLoad in the main view controller that it creates:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        CABasicAnimation *rotationAnimation = [CABasicAnimation 
                             animationWithKeyPath:@"transform.rotation.z"];
    
        [rotationAnimation setFromValue:DegreesToNumber(0)];
        [rotationAnimation setToValue:DegreesToNumber(360)];
        [rotationAnimation setDuration:3.0f];
        [rotationAnimation setRepeatCount:10000];
    
        [[[self view] layer] addAnimation:rotationAnimation forKey:@"rotate"];
    }
    

    This will rotate the view around the x axis (center by default). You can just set the contents of that view (e.g. [[self view] setContents:image] where image is an image that you load using UIImage).

    Here are my helper functions for converting degrees to radians.

    CGFloat DegreesToRadians(CGFloat degrees)
    {
        return degrees * M_PI / 180;
    }
    
    NSNumber* DegreesToNumber(CGFloat degrees)
    {
        return [NSNumber numberWithFloat:
                DegreesToRadians(degrees)];
    }
    

    Hope that helps.

提交回复
热议问题