CGAffineTransform重置

Giz*_*odo 38 iphone rotation reset cgaffinetransform

3我有一个被触摸操纵的图像.

让我们说它是一个指向箭头的图像.在它旋转180度后,箭头现在指向下方,我想重置CGAffineTransform属性,以便它现在认为它转回0度.

我想要这个,因为我必须翻译,旋转,缩放等,无论图像的角度是0还是180.

提前致谢.

EDITED Ater 5回复:

嗯,我不确定这些答案是否符合我的要求.我为不清楚而道歉.

  1. 触摸箭头指向上方的图像并向右移动 - 一切都很好.
  2. 用旋转手势旋转图像并转动180度 - 一切都很好
  3. 图像现在被触摸并向右移动 - 由于坐标是颠倒的,现在图像上下跳跃使我的其他动画序列变得棘手.

为了防止上述问题和其他问题,我希望在旋转180后重置所有内容.例如,一旦图像变为180度,CGAffineTransform属性就知道它已经转为180%.我想在那个时候重置这些属性,所以CGAffineTransform认为它变为0度而不是180度,尽管图像是视觉上倒置的.我希望这种情况在没有任何视觉变化的情况下发生,一旦旋转到180度.

希望这更清楚......

Mic*_*ine 77

如果您尝试重置转换,以便图像按原样显示,则只需将转换设置回标识即可.

self.imageView.transform = CGAffineTransformIdentity
Run Code Online (Sandbox Code Playgroud)

如果要对变换后的图像应用任意变换,最简单的方法是使用接收现有变换的CGAffineTransform方法.只需发送现有的转型.例如:

CGAffineTransform scale = CGAffineTransformMakeScale(zoom, 1);
self.imageView.transform = CGAffineTransformConcat(self.imageView.transform, scale);
Run Code Online (Sandbox Code Playgroud)

如果您真的需要图像,而根本没有任何变换,则必须将其绘制回另一个图像.这也不是那么难,但我不推荐它作为你的第一个解决方案.此代码适用于任意视图的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;
}
Run Code Online (Sandbox Code Playgroud)


sky*_*ook 39

Swift 3Swift 4

您可以使用以下命令重置CGAffineTransform:

self.imageView.transform = CGAffineTransform.identity
Run Code Online (Sandbox Code Playgroud)


Gur*_*ngh 5

昨天做了类似但更简单的事情.

查找任何现有的转换值.然后,将其作为新变换的偏移量.

见例子:

// 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;
Run Code Online (Sandbox Code Playgroud)
// 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;
Run Code Online (Sandbox Code Playgroud)

如果您希望将现有变换值添加到新变换以构成正确的移动,这只是一个提示.使用相同的方法进行比例等

一些人建议使用CABasicAnimation,将附加属性设置为YES.但无法让它发挥作用.