简体   繁体   中英

rotate image 90 degrees around anchor point

I want to rotate my imageview 90 degrees around an anchor point. You can see what I want to achieve over here. 在此处输入图片说明

I've got this piece of code.

CGFloat DegreesToRadians(CGFloat degrees)
{
    return degrees * M_PI / 180;
}

NSNumber* DegreesToNumber(CGFloat degrees)
{
    return [NSNumber numberWithFloat:
            DegreesToRadians(degrees)];
}
-(IBAction)rotate:(id)sender{
    CABasicAnimation *rotationAnimation = [CABasicAnimation
                                           animationWithKeyPath:@"transform.rotation.y"];

    [rotationAnimation setFromValue:DegreesToNumber(0)];
    [rotationAnimation setToValue:DegreesToNumber(90)];
    [rotationAnimation setDuration:3.0f];
    [rotationAnimation setRepeatCount:1];

    [[[self imgShare] layer] addAnimation:rotationAnimation forKey:@"rotate"];
}

There are a view problems. First when it rotates 90 degrees it immedialty flips back to its original state. Also I don't know how I can turn it arround an anchor point.

Any help ?

尝试这个:

self.imgShare.transform = CGAffineTransformMakeRotation(M_PI/2);

You are using a CABAsicAnimation which just handles the animation part. To rotate your view after the rotation is your responsibility. Therefore you should change the transform property of the view after the rotation.

self.imgShare.transform = CGAffineTransformMakeRotation(M_PI/2);

For your problem with the anchorPoint . Every UIView is backed by a CALayer and every CALayer has an anchorPoint property. Just change the views anchorpoint and you will be able to rotate the view around that point.

The anchorPoint defaults to 0.5,0.5. Change it between 0.0 and 1.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM