美文网首页
Core Animation动画键值和代理 Swift3.0

Core Animation动画键值和代理 Swift3.0

作者: 断忆残缘 | 来源:发表于2016-11-28 00:08 被阅读234次

前言

UIKit动画缺点:一旦你创建并运行视图动画,你不能暂停,停止或以任何方式访问它。
Core Animation动画优势:可以轻松检查在图层上运行的动画,并在需要时停止它们。此外,还可以设置动画委托对象,接收动画开始和结束(或中断)时的委托回调。

正文

1. 介绍动画代理

动画代理(CAAnimationDelegate)的两个方法

func animationDidStart(_ anim: CAAnimation)
func animationDidStop(_ anim: CAAnimation, finished flag: Bool)
屏幕快照 2016-11-27 下午11.34.14.png
  // 初始化layer动画
  let flyRight = CABasicAnimation(keyPath: "position.x")
  flyRight.toValue = view.bounds.size.width/2
  flyRight.fromValue = -view.bounds.size.width/2
  flyRight.duration = 0.5
  flyRight.delegate = self // 设置代理

extension ViewController: CAAnimationDelegate {
  // 动画开始回调
  func animationDidStart(_ anim: CAAnimation) {
    print("animation did start")
  }
  // 动画结束回调
  func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    print("animation did finish")
  }
}
2. 设置动画键值

设置动画键值,等同于Dictionary字典一样。

  // 设置动画键值
  flyRight.setValue("form", forKey: "name")
  flyRight.setValue(heading.layer, forKey: "layer")

  
 extension ViewController: CAAnimationDelegate {
  // 在回调中根据键值获取动画,并新增动画
  func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    print("animation did finish")
    guard let name = anim.value(forKey: "name") as? String else {
      return
    }
    
    if name == "form" {
      let layer = anim.value(forKey: "layer") as? CALayer
      anim.setValue(nil, forKey: "layer")
      
      let pulse = CABasicAnimation(keyPath: "transform.scale")
      pulse.fromValue = 1.25
      pulse.toValue = 1.0
      pulse.duration = 0.25
      layer?.add(pulse, forKey: nil)
    }
  }
}
3. 组合复杂动画
  // 创建动画控件
  info.frame = CGRect(x: 0.0, y: loginButton.center.y + 60.0,
                      width: view.frame.size.width, height: 30)
  info.backgroundColor = UIColor.clear
  info.font = UIFont(name: "HelveticaNeue", size: 12.0)
  info.textAlignment = .center
  info.textColor = UIColor.white
  info.text = "Tap on a field and enter username and password"
  view.insertSubview(info, belowSubview: loginButton)

  // 增加第一个动画
  let flyLeft = CABasicAnimation(keyPath: "position.x")
  flyLeft.fromValue = info.layer.position.x + view.frame.size.width
  flyLeft.toValue = info.layer.position.x
  flyLeft.duration = 5.0
  info.layer.add(flyLeft, forKey: "infoappear")
  // 增加第二个动画
  let fadeLabelIn = CABasicAnimation(keyPath: "opacity")
  fadeLabelIn.fromValue = 0.2
  fadeLabelIn.toValue = 1.0
  fadeLabelIn.duration = 4.5
  info.layer.add(fadeLabelIn, forKey: "fadein")
4. 移除终止动画

移除动画方法

func removeAllAnimations() // 移除所有动画
func removeAnimation(forKey key: String) // 移除单个动画

demo

extension ViewController: UITextFieldDelegate {
  func textFieldDidBeginEditing(_ textField: UITextField) {
    print(info.layer.animationKeys() ?? "")
    // 移除所有动画
    info.layer.removeAllAnimations() 
  }
}

相关文章

网友评论

      本文标题:Core Animation动画键值和代理 Swift3.0

      本文链接:https://www.haomeiwen.com/subject/bsgnpttx.html