将图像旋转到锚点周围90度

时间:2021-07-26 18:15:39

I want to rotate my imageview 90 degrees around an anchor point. You can see what I want to achieve over here.将图像旋转到锚点周围90度

我想将我的imageview旋转90度绕锚点。你可以在这里看到我想要达到的目标。

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.

有一个观点问题。首先,当它旋转90度时,它立即翻转回其原始状态。另外我不知道怎么能把它转到一个锚点。

Any help ?

有帮助吗?

2 个解决方案

#1


1  

Try this:

尝试这个:

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

#2


0  

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.

您正在使用仅处理动画部分的CABAsicAnimation。旋转后轮换您的视图是您的责任。因此,您应该在旋转后更改视图的transform属性。

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.

对于你的anchorPoint问题。每个UIView都有一个CALayer支持,每个CALayer都有一个anchorPoint属性。只需更改视图锚点,您就可以围绕该点旋转视图。

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

anchorPoint默认为0.5,0.5。将其更改为0.0到1.0之间。

#1


1  

Try this:

尝试这个:

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

#2


0  

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.

您正在使用仅处理动画部分的CABAsicAnimation。旋转后轮换您的视图是您的责任。因此,您应该在旋转后更改视图的transform属性。

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.

对于你的anchorPoint问题。每个UIView都有一个CALayer支持,每个CALayer都有一个anchorPoint属性。只需更改视图锚点,您就可以围绕该点旋转视图。

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

anchorPoint默认为0.5,0.5。将其更改为0.0到1.0之间。