【问题标题】:create a custom view programmatically then animate it以编程方式创建自定义视图,然后对其进行动画处理
【发布时间】:2017-06-05 19:23:56
【问题描述】:

我正在以编程方式创建一个自定义视图,它工作正常,然后我正在尝试为视图设置动画。出于某种奇怪的原因,视图完全是动画的,我不知道为什么。代码如下:

  override func viewDidLoad() {
    super.viewDidLoad()


    let customView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))

    customView.backgroundColor = UIColor.blue
    customView.layer.cornerRadius = 25
    customView.layer.borderWidth = 8
    customView.layer.borderColor = UIColor.red.cgColor

    self.view.addSubview(customView)

    UIView.animate(withDuration: 4, animations: {

      customView.transform.translatedBy(x: 40, y: 60)
      customView.transform.rotated(by: CGFloat.pi/2)
      customView.transform.scaledBy(x: 0, y: 0.5)

    })
  }

【问题讨论】:

    标签: ios swift uiview transform uianimation


    【解决方案1】:

    你最好在 viewDidAppear 中调用这个动画块,但有一些延迟。

    import UIKit
    
    class ViewController: UIViewController {
    
        var customView = UIView()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            customView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
            customView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
    
            customView.backgroundColor = UIColor.blue
            customView.layer.cornerRadius = 25
            customView.layer.borderWidth = 8
            customView.layer.borderColor = UIColor.red.cgColor
    
            self.view.addSubview(customView)
        }
    
        override func viewDidAppear(_ animated: Bool) {
            UIView.animate(withDuration: 5, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
              self.customView.transform = CGAffineTransform(scaleX: 1, y: 0.5)
              self.customView.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
              self.customView.transform = CGAffineTransform(translationX: 40, y: 60)
            }, completion: nil)
        }
    
        func handleTap() {
            UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.customView.transform = CGAffineTransform(scaleX: 1, y: 0.5)
                self.customView.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
                self.customView.transform = CGAffineTransform(translationX: 40, y: 60)
            }, completion: nil)
        }
    
    }
    

    【讨论】:

    • 真的帮了我!!谢谢!!
    【解决方案2】:

    您的问题不正确。你的视图没有动画。

    为什么?

    您正在使用返回函数,而您的 customView 需要像这样捕获修改后的版本。

    customView.transform =  customView.transform.translatedBy(x: 40, y: 60)
    

    所以你的代码会是这样的。

    让 customView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))

        customView.backgroundColor = UIColor.blue
        customView.layer.cornerRadius = 25
        customView.layer.borderWidth = 8
        customView.layer.borderColor = UIColor.red.cgColor
    
        self.view.addSubview(customView)
    
        UIView.animate(withDuration: 4, animations: {
    
           customView.transform =  customView.transform.translatedBy(x: 40, y: 60)
           //customView.transform = customView.transform.rotated(by: CGFloat.pi/2)
           //customView.transform = customView.transform.scaledBy(x: 0, y: 0.5)
    
        })
    

    网上有很多关于动画分组的资源。你google一下吧。:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 2012-03-12
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多