CGAffineTransform重置

发布于 2024-11-07 06:50:33 字数 613 浏览 0 评论 0原文

3我有一个可以通过触摸操作的图像。

假设它是一个向上的箭头图像。旋转 180 度后,箭头现在指向下,我想重置 CGAffineTransform 属性,以便它认为现在已转回 0 度。

我想要这个,因为我必须平移、旋转、缩放等,无论图像的角度是 0 还是 180。

提前致谢。

编辑后 5 条回复:

嗯,我不确定这些答案是否符合我的要求。我很抱歉没有说清楚。

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

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

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

3I have an image that gets manipulated by touch.

Lets say its an image of an arrow that points up. After it's been rotated 180 degrees, so the arrow is pointing down now, I want to reset CGAffineTransform properties so that it thinks now its turned back to 0 degrees.

I want this because I have to translate, rotate, scale etc whether the image's angle is 0 OR 180.

Thanks in advance.

EDITED Ater 5 replies:

Hmm, I am not sure if any of these answers do what I wanted to. I apologize for not being clear.

  1. An Image with the arrow pointing up is touched and moved to right - everything is fine.
  2. Image is rotated with the rotation gesture and turned 180deg - everything is fine
  3. Image is now touched and moved to right - Since the coordinates are upside down, now the image is jumping up and down making my other animation sequences go screwy.

To prevent above problem and some other ones, I want to reset everything after it has been rotated 180. For example once the image turned 180deg, CGAffineTransform properties knows it's turned 180%. I want to reset the poperties at that time so CGAffineTransform thinks its turned 0 degrees instead of 180, eventhough the image is upside down visually. I want this to happen without any visual changes, soon as its rotated to 180deg.

Hope this is clearer....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神回复 2024-11-14 06:50:33

如果您尝试重置变换,以使图像像原来一样显示,则只需将变换设置回恒等式即可。

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

If you are trying to reset the transformation, so that the image appears as it did originally, you can simply set the transform back to the identity.

self.imageView.transform = CGAffineTransformIdentity

If you want to apply arbitrary transformations to the transformed image, the easiest thing to do would be to use the CGAffineTransform methods that take in an existing transformation. Just send in the existing transformation. For example:

CGAffineTransform scale = CGAffineTransformMakeScale(zoom, 1);
self.imageView.transform = CGAffineTransformConcat(self.imageView.transform, scale);

If you REALLY need the image as it appears without any transformation at all, you'll have to draw it back into another image. That's not that hard either, but I don't recommend it as your first solution. This code works in the context of a UIView category for an arbitrary view, including a 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;
}
揪着可爱 2024-11-14 06:50:33

Swift 3Swift 4Swift 5

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

self.imageView.transform = CGAffineTransform.identity

较短的版本只是 .identity 像这样:

self.imageView.transform = .identity

Swift 3, Swift 4 and Swift 5

You can reset a CGAffineTransform with this:

self.imageView.transform = CGAffineTransform.identity

The shorter version is just .identity like so:

self.imageView.transform = .identity
葬﹪忆之殇 2024-11-14 06:50:33

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

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

查看示例:

// 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。但无法让它发挥作用。

Did something similar but simpler yesterday.

Find any existing transform value. Then, put that as an offset to the new transform.

See examples:

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

This is just a hint if you want to live with adding up existing transform values to new transforms to make up the right move. Use same methods for scale, etc.

Some suggested to use CABasicAnimation with additive property set to YES. But couldn't get that to work.

自控 2024-11-14 06:50:33

目标C:

self.transform = CGAffineTransformIdentity;

Objective C:

self.transform = CGAffineTransformIdentity;
爺獨霸怡葒院 2024-11-14 06:50:33

您可以组合多个转换。例如:

CGAffineTransform rotation = CGAffineTransformMakeRotation(up ? 0 : M_PI);
imageView.transform = CGAffineTransformScale(rotation,xScale, yScale);

You can combine multiple transformations. For example:

CGAffineTransform rotation = CGAffineTransformMakeRotation(up ? 0 : M_PI);
imageView.transform = CGAffineTransformScale(rotation,xScale, yScale);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文