【问题标题】:CGAffineTransform ResetCGAffineTransform 重置
【发布时间】:2011-05-13 18:00:23
【问题描述】:

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

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

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

提前致谢。

已编辑 Ater 5 回复:

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

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

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

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

【问题讨论】:

    标签: iphone rotation reset cgaffinetransform


    【解决方案1】:

    如果您尝试重置转换,以使图像显示为原来的样子,您只需将转换设置回身份即可。

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

    【讨论】:

    • 我认为他想要的是让它保持向下,但这就是身份转换。
    【解决方案2】:

    Swift 3Swift 4Swift 5

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

    self.imageView.transform = CGAffineTransform.identity
    

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

    self.imageView.transform = .identity
    

    【讨论】:

      【解决方案3】:

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

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

      查看示例:

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

      【讨论】:

        【解决方案4】:

        目标 C:

        self.transform = CGAffineTransformIdentity;
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

          • 您对 Micah 回答的评论是正确的。但是这个答案也没有解决这个问题。我目前面临类似的需求。您还有什么建议吗?
          • 如果你真的在寻找这个问题的答案,你可以开始赏金。
          猜你喜欢
          • 1970-01-01
          • 2016-03-24
          • 1970-01-01
          • 2010-10-08
          • 2012-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多