简体   繁体   English

旋转UIImageView将图像移出屏幕

[英]Rotating UIImageView Moves the Image Off Screen

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. 我在代码中实现了一个简单的旋转手势,但是问题是,当我旋转图像时,它总是向右偏离屏幕/视图。

The image view that is being rotated center X gets off or increases (hence it going right off the screen out of the view). 中心X旋转的图像视图将关闭或增加(因此它会从屏幕上移出视图)。

I would like it to rotate around the current center, but it's changing for some reason. 我希望它绕当前中心旋转,但是由于某些原因它正在改变。 Any ideas what is causing this? 任何想法是什么原因造成的?

Code Below: 下面的代码:

- (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. 请注意,旋转后,您可能必须撤消平移变换才能正确绘制图像。

Hope this helps 希望这可以帮助

Edit: 编辑:

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);

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. 但是,在创建了这个快速项目之后,我意识到,当您使用UIKit应用旋转变换时(就像您显然进行的方式一样),旋转实际上围绕中心进行。 It is only when using CoreGraphics that the rotation happens around the origin. 仅当使用CoreGraphics时,旋转才围绕原点发生。 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. 如果您还有其他问题,请告诉我。

The 'Firefox' image is drawn using UIKit. “ Firefox”图像是使用UIKit绘制的。 The blue rect is drawn using CoreGraphics 蓝色矩形是使用CoreGraphics绘制的 “ Firefox”图像是使用UIKit绘制的。蓝色矩形是使用CoreGraphics绘制的

You aren't rotating the image around its centre. 您不会围绕图像的中心旋转图像。 You'll need correct this manually by translating it back to the correct position 您需要手动纠正此问题,方法是将其平移回正确的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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