3我有一个被触摸操纵的图像。
让我们说它是一个指向箭头的图像。在它旋转180度后,箭头现在指向下方,我想重置CGAffineTransform属性,以便它现在认为它转回0度。
我想要这个,因为我必须翻译,旋转,缩放等,无论图像的角度是0还是180。
提前致谢。
编辑5回复后:
嗯,我不确定这些答案是否符合我的要求。我为不清楚而道歉。
为了防止上述问题和其他问题,我希望在旋转180后重置所有内容。例如,一旦图像变为180度,CGAffineTransform属性就知道它已经转为180%。我想在那个时候重置这些属性,所以CGAffineTransform认为它变为0度而不是180度,尽管图像是视觉上倒置的。我希望这种情况在没有任何视觉变化的情况下发生,一旦旋转到180度。
希望这更清楚......
如果您尝试重置转换,以便图像按原样显示,则只需将转换设置回标识即可。
self.imageView.transform = CGAffineTransformIdentity
如果要对变换后的图像应用任意变换,最简单的方法是使用接收现有变换的CGAffineTransform方法。只需发送现有的转型。例如:
CGAffineTransform scale = CGAffineTransformMakeScale(zoom, 1);
self.imageView.transform = CGAffineTransformConcat(self.imageView.transform, scale);
如果您真的需要图像,而根本没有任何变换,则必须将其绘制回另一个图像。这也不是那么难,但我不推荐它作为你的第一个解决方案。此代码适用于任意视图的UIView类别的上下文,包括UIImageView:
- (UIImage *) capture {
CGRect screenRect = self.frame;
CGFloat scale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(screenRect.size, YES, scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.layer renderInContext: ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Swift 3,Swift 4和Swift 5
您可以使用以下命令重置CGAffineTransform:
self.imageView.transform = CGAffineTransform.identity
较短的版本就像.identity
一样:
self.imageView.transform = .identity
昨天做了类似但更简单的事情。
查找任何现有的转换值。然后,将其作为新变换的偏移量。
见例子:
// Rotate right/clockwise
CGFloat radians = atan2f(imageView.transform.b, imageView.transform.a);
CGFloat degrees = radians * (180 / M_PI);
// Yeah yeah, I like degrees.
CGAffineTransform transform = CGAffineTransformMakeRotation((90 + degrees) * M_PI/180);
imageView.transform = transform;
// Rotate left/anticlockwise
CGFloat radians = atan2f(imageView.transform.b, imageView.transform.a);
CGFloat degrees = radians * (180 / M_PI);
// Yeah yeah, I like degrees.
CGAffineTransform transform = CGAffineTransformMakeRotation((-90 + degrees) * M_PI/180);
imageView.transform = transform;
如果您希望将现有变换值添加到新变换以构成正确的移动,这只是一个提示。使用相同的方法进行比例等
一些人建议使用CABasicAnimation,将附加属性设置为YES。但无法让它发挥作用。
您可以组合多个转换。例如:
CGAffineTransform rotation = CGAffineTransformMakeRotation(up ? 0 : M_PI);
imageView.transform = CGAffineTransformScale(rotation,xScale, yScale);
目标C:
self.transform = CGAffineTransformIdentity;