【问题标题】:animate rotation UIImageView in swift快速动画旋转UIImageView
【发布时间】:2015-10-07 08:10:21
【问题描述】:

我正在尝试在 Swift 中为 UIImageView 旋转 180 度设置动画

    UIView.animateWithDuration(1.0, animations: { () -> Void in
        self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
    }) { (succeed) -> Void in

    }

但根本没有动画。 我想使用 animateWithDuration 因为我想在某个时候使用CGAffineTransformMakeIdentity 恢复它

使用UIView 动画作品。

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIView.setAnimationCurve(UIViewAnimationCurve.EaseIn)
    let radians = CGFloat(180 * M_PI / 180)
    arrowImageView.transform = CGAffineTransformMakeRotation(radians)
    UIView.commitAnimations()

【问题讨论】:

标签: ios swift


【解决方案1】:

======= UIView 的扩展 =======

我从互联网上找到了这个答案,并对其进行了测试,效果很好。希望这对任何人都有帮助,只需将 UIView 更改为您的需要,例如 UIView、UIButton、UIImageView 等,并使用 myUIView.rotate() 访问 函数,这里的 myUIView 应该 replaceyour View name(IBOutlet -> name)和正确的视图类型进行扩展。这是我找到这个答案的link。而且这种方法简单有效!

=== SWIFT 3 / 4 ===

extension UIView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(double: M_PI * 2)
        rotation.duration = 1
        rotation.cumulative = true
        rotation.repeatCount = FLT_MAX
        self.layer.addAnimation(rotation, forKey: "rotationAnimation")
    }
}

建议的改进。感谢所有建议。

=== SWIFT 5 ===

extension UIView{
    func rotate() {
        let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = NSNumber(value: Double.pi * 2)
        rotation.duration = 1
        rotation.isCumulative = true
        rotation.repeatCount = Float.greatestFiniteMagnitude
        self.layer.add(rotation, forKey: "rotationAnimation")
    }
}

然后像下面这样调用那个扩展:

self.your_UIViewOutletName_myUIView_here.rotate()

【讨论】:

  • 在 swift 4 中,M_PI 已替换为 Double.piFLT_MAX 已替换为 Float .greatestFiniteMagnitude,以及 cumulativeisCumulative :)
  • 请注意:这个扩展可以被移除,并且只能被修改用于功能用途,我创建了这个扩展,因为你可以在任何地方使用它并具有可重用性,你可以将它用于任何类型的视图类型:UIImageView,UIView..等
  • 您很可能不需要更改正在扩展的类 - 您提到的 3 个类与许多其他类一样从 UIView 继承
【解决方案2】:

这是旋转图像视图的代码。迅捷3.0

UIView.animate(withDuration:2.0, animations: {
            self.dropDownImage.transform = CGAffineTransform(rotationAngle: CGFloat(imageRotation))
        })

【讨论】:

    【解决方案3】:

    更新了swift 4.0版本:

        let rotation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotation.toValue = Double.pi * 2
        rotation.duration = 0.25 // or however long you want ...
        rotation.isCumulative = true
        rotation.repeatCount = Float.greatestFiniteMagnitude
        yourView.layer.add(rotation, forKey: "rotationAnimation")
    

    【讨论】:

      【解决方案4】:

      首先,如果要旋转 180 度,则必须转换为弧度。 180 度的弧度是pi。 360 度将是2 * pi180 * pi 将使您的动画在一秒钟内旋转大约 90 次,并以原始方向结束。

      其次,我不确定您的代码为什么不起作用,但我知道下面的代码确实起作用:

      let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
      rotationAnimation.fromValue = 0.0
      rotationAnimation.toValue = M_PI
      rotationAnimation.duration = 1.0
      
      self.arrowImageView.layer.addAnimation(rotationAnimation, forKey: nil)
      

      【讨论】:

        【解决方案5】:

        Swift 5.0

        extension UIImageView{
            func rotate() {
                let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
                rotation.toValue = NSNumber(value: Double.pi * 2)
                rotation.duration = 2
                rotation.isCumulative = true
                rotation.repeatCount = Float.greatestFiniteMagnitude
                self.layer.add(rotation, forKey: "rotationAnimation")
            }
        }
        

        更新到@Rakshitha Muranga Rodrigo 的答案。

        【讨论】:

        • 很好的建议谢谢。一位开发人员也在帖子中提出了建议,所以我现在也将其包括在内......干杯!
        【解决方案6】:

        您在CGFloat(180 * M_PI) 上遗漏了/ 180。试试:

        UIView.animate(withDuration: 1.0) {[weak self] in
            self?.arrowImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))
        }
        

        【讨论】:

          【解决方案7】:

          90度旋转

          var shouldRotateArrow = false {
              didSet {
                  rotateArrow()
              }
          }
          
          private func rotateArrow() {
              let angle: CGFloat = shouldRotateArrow ? .pi / 2 : 0
              UIView.animate(withDuration: Constants.arrowRotationDuration) {
                  self.arrowImageView.transform = CGAffineTransform(rotationAngle: angle)
              }
          }
          

          【讨论】:

            【解决方案8】:

            一定要在主线程中

            DispatchQueue.main.async {
               UIView.animateWithDuration(1.0, animations: { () -> Void in
                 self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
               }) { (succeed) -> Void in
            
               }
            }
            

            【讨论】:

              【解决方案9】:

              对我来说最好和最短的解决方案是(Swift 3/4):

              self.YourImageView.transform = showing ? CGAffineTransform(rotationAngle: CGFloat.pi) : CGAffineTransform.identity
              

              “显示”它是一个确定图像之前是否旋转过的值 - 您可以在此处使用自己的逻辑

              【讨论】:

                【解决方案10】:

                以下代码有助于连续旋转视图。 UIView animate with .repeat mode 无限重复动画。

                开始旋转 ImageView ------------------

                func startSpinning()  {
                            UIView.animate(withDuration: 0.5, delay: 0, options: .repeat, animations: { () -> Void in
                                self.gearImageView!.transform = self.gearImageView!.transform.rotated(by: .pi / 2)
                                })
                        }
                
                
                
                
                To stop spin ImageView 
                --------------------
                
                func stopSpinning()  {
                        self.gearImageView.layer.removeAllAnimations()
                    }
                

                【讨论】:

                • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
                【解决方案11】:

                尝试调用arrowImageView.layoutSubviews(),见下图:

                    UIView.animateWithDuration(1.0, animations: { () -> Void in
                        self.arrowImageView.transform = CGAffineTransformMakeRotation(CGFloat(180 * M_PI))
                        self.arrowImageView.layoutSubviews()
                    }) { (succeed) -> Void in
                
                    }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-03
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多