旋转UIImageView将图像移出屏幕 [英] Rotating UIImageView Moves the Image Off Screen

查看:54
本文介绍了旋转UIImageView将图像移出屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中实现了一个简单的旋转手势,但是问题是,当我旋转图像时,它总是在屏幕外/视图外向右移动.

I have a simple rotation gesture implemented in my code, but the problem is when I rotate the image it goes off the screen/out of the view always to the right.

以中心X旋转的图像视图会关闭或增加(因此它会从屏幕上移出视图).

The image view that is being rotated center X gets off or increases (hence it going right off the screen out of the view).

我希望它绕当前中心旋转,但是由于某些原因它正在改变.任何想法是什么原因造成的?

I would like it to rotate around the current center, but it's changing for some reason. Any ideas what is causing this?

以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CALayer *l = [self.viewCase layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:30.0];

    self.imgUserPhoto.userInteractionEnabled = YES;
    [self.imgUserPhoto setClipsToBounds:NO];

    UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationDetected:)];
    [self.view addGestureRecognizer:rotationRecognizer];

    rotationRecognizer.delegate = self;
}

- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer
{
    CGFloat angle = rotationRecognizer.rotation;
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, angle);
    rotationRecognizer.rotation = 0.0;
}

推荐答案

您要围绕图像的中心旋转图像,但这并不是实际发生的情况.旋转变换围绕原点进行.因此,您要做的是首先应用平移变换将原点映射到图像的中心,然后应用旋转变换,如下所示:

You want to rotate the image around it's center, but that's not what it is actually happening. Rotation transforms take place around the origin. So what you have to do is to apply a translate transform first to map the origin to the center of the image, and then apply the rotation transform, like so:

self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, self.imageView.bounds.size.width/2, self.imageView.bounds.size.height/2);

请注意,旋转后,您可能必须撤消平移变换才能正确绘制图像.

Please note that after rotating you'll probably have to undo the translate transform in order to correctly draw the image.

希望这会有所帮助

要快速回答您的问题,撤消转换变换"的操作是首先减去您添加到其中的相同差,例如:

To quickly answer your question, what you have to do to undo the Translate Transform is to subtract the same difference you add to it in the first place, for example:

// The next line will add a translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 10, 10);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, radians);
// The next line will undo the translate transform
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -10, -10);

但是,在创建此快速项目之后我意识到,当您使用UIKit应用旋转变换时(就像您显然进行的方式一样),旋转实际上围绕中心发生.仅当使用CoreGraphics时,旋转才围绕原点发生.所以现在我不确定为什么您的图像不在屏幕上.无论如何,请看一下该项目,看看那里的代码是否对您有帮助.

However, after creating this quick project I realized that when you apply a rotation transform using UIKit (like the way you're apparently doing it) the rotation actually takes place around the center. It is only when using CoreGraphics that the rotation happens around the origin. So now I'm not sure why your image goes off the screen. Anyway, take a look at the project and see if any code there helps you.

让我知道您是否还有其他问题.

Let me know if you have any more questions.

"Firefox"图像是使用UIKit绘制的.蓝色矩形是使用CoreGraphics绘制的

The 'Firefox' image is drawn using UIKit. The blue rect is drawn using CoreGraphics

这篇关于旋转UIImageView将图像移出屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆