【问题标题】:rotate 180 degree for an UIImageView, and rotate back with same route为 UIImageView 旋转 180 度,并以相同的路线旋转回来
【发布时间】:2015-02-11 06:05:41
【问题描述】:

我有一个按钮,想旋转里面的图片:

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);

图片在用户点击后是向上箭头到向下箭头 再次单击按钮时,我希望向下箭头再次旋转为向上箭头,但它是相同的逆时针旋转,但我只是希望箭头反向回到向上箭头

回滚代码:

self.DashButton.imageView.transform = CGAffineTransformIdentity;

我试过了

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(-M_PI);

我想要的不是旋转整圈,只是旋转 180 度和 -180 度旋转,而不是 360 度旋转

【问题讨论】:

  • CGAffineTransformMakeRotation(-0);?
  • 我已经回答完了。看看这个。 -0 没有帮助

标签: ios objective-c swift xcode core-animation


【解决方案1】:

使用 -3.14159 代替 -M_PI。有点诡计,但下面的代码拯救了我的一天。

self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI - 3.14159);

【讨论】:

  • 这在 CoreAnimation 计算最短变换时起作用,在使用圆角 pi 时,最短变换是反转旋转。然而,在实践中,旋转似乎与使用 pi 没有区别。
【解决方案2】:
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
self.DashButton.imageView.transform = CGAffineTransformMakeRotation(0);

【讨论】:

  • 欢迎来到 Stack Overflow!尽管此代码可能有助于解决问题,但它并没有解释 why 和/或 如何 回答问题。提供这种额外的背景将显着提高其长期价值。请edit您的答案添加解释,包括适用的限制和假设。
【解决方案3】:

你需要的是一个负角来向相反的方向旋转

CGAffineTransformMakeRotation(180 * CGFloat(M_PI/180))

CGAffineTransformMakeRotation(-1 * CGFloat(M_PI/180))

查看这个答案以获得解释About CGAffineTransformMakeRotation rotation direction?

【讨论】:

    【解决方案4】:

    Swift 4 版本的@Maria 工作答案

    逆时针旋转180度:

    let transform = CGAffineTransform.identity
    
    UIView.animate(withDuration: 0.3, animations: {
          // to rotate counterclockwise set both of these in this exact order
          self.button.transform = transform.rotated(by: 180 * CGFloat(Double.pi))
          self.button.transform = transform.rotated(by: -1 * CGFloat(Double.pi))
    }
    

    顺时针旋转180度:

    UIView.animate(withDuration: 0.3, animations: { 
          self.button.transform = CGAffineTransform.identity
    }
    

    【讨论】:

      【解决方案5】:

      试试这个

      arrowLeft.transform = CGAffineTransformMakeRotation(180 *M_PI / 180.0);
      
      arrowLeft.transform = CGAffineTransformMakeRotation(0*M_PI/180);
      

      【讨论】:

      • 不,它和我的代码一样。你试过了吗?我想要的不是旋转整圈,只是旋转 180 度和 -180 度旋转,而不是 360 度旋转
      • 思路没有错,但是旋转0还是顺时针,我要相反的时钟。
      • @Wingzero 您想为更改设置动画吗?
      • 看我的回答,只想要一个 180 度和一个 -180 旋转回来。
      【解决方案6】:

      也可以使用

      imageView.image = UIImage(cgImage: imageView.image!.cgImage! , scale: 1.0, orientation: .down)
      

      【讨论】:

        【解决方案7】:

        这个怎么样?

        self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        self.DashButton.imageView.transform = CGAffineTransformMakeRotation(M_PI * 2);
        

        它适用于我的代码:)

        【讨论】:

          【解决方案8】:

          斯威夫特 3:

          使用CABasicAnimation

          var rotationAnimation = CABasicAnimation()
          rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
          rotationAnimation.toValue = NSNumber(value: (Double.pi))
          rotationAnimation.duration = 1.0
          rotationAnimation.isCumulative = true
          rotationAnimation.repeatCount = 100.0
          view.layer.add(rotationAnimation, forKey: "rotationAnimation")
          


          这是用于处理启动和停止旋转操作的 UIView 的 extension 函数:

          extension UIView {
          
              func startRotation() {
                  let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
                  rotation.fromValue = 0
                  rotation.toValue = NSNumber(value: Double.pi)
                  rotation.duration = 1.0
                  rotation.isCumulative = true
                  rotation.repeatCount = FLT_MAX
                  self.layer.add(rotation, forKey: "rotationAnimation")
              }
          
              func stopRotation() {
                  self.layer.removeAnimation(forKey: "rotationAnimation")
              }
          }
          


          现在使用,UIView.animation 闭包:

          UIView.animate(withDuration: 0.5, animations: { 
                button.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))) 
          }) { (isAnimationComplete) in
              // Animation completed 
          }
          

          【讨论】:

            猜你喜欢
            • 2016-11-02
            • 1970-01-01
            • 1970-01-01
            • 2011-09-20
            • 1970-01-01
            • 2011-11-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多