我在我的代码中实现了一个简单的旋转手势,但问题是当我旋转图像时,它会离开屏幕/视图总是向右。
旋转中心X的图像视图离开或增加(因此它正好离开屏幕)。
我希望它围绕当前的中心旋转,但由于某种原因,它正在发生变化。你知道是什么原因造成的吗?
代码如下:
- (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;
}
发布于 2013-06-14 20:38:02
你想围绕它的中心旋转图像,但这并不是实际发生的事情。旋转变换围绕原点进行。因此,您需要做的是首先应用平移变换以将原点映射到图像的中心,然后应用旋转变换,如下所示:
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, self.imageView.bounds.size.width/2, self.imageView.bounds.size.height/2);
请注意,在旋转之后,为了正确绘制图像,您可能必须撤消平移变换。
希望这能有所帮助
编辑:
要快速回答您的问题,要撤消平移变换,必须减去最初添加到平移变换中的相同差,例如:
// 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);
然而,在创建this quick project之后,我意识到当您使用UIKit应用旋转变换时(就像您显然正在做的那样),旋转实际上是围绕中心发生的。仅当使用CoreGraphics时,才会围绕原点进行旋转。所以现在我不确定为什么你的图像会从屏幕上消失。无论如何,看一看这个项目,看看有没有什么代码能帮到你。
如果你还有什么问题,请告诉我。
“Firefox”图像是使用UIKit绘制的。蓝色矩形是使用CoreGraphics绘制的
发布于 2013-06-14 22:56:28
你不能绕着图像中心旋转图像。您需要通过将其转换回正确的位置来手动进行更正
https://stackoverflow.com/questions/17116459
复制相似问题