键值编码在CABasicAnimation

时间:2015-10-19 18:38:31

标签: ios swift animation cabasicanimation key-value-coding

我目前正在尝试创建一个具有基于触摸功能的自定义UIControl。我的问题是,当我调用transform方法时,opacityfunc setValue forKeyPath 键路径不会被分开,尽管图层动画完全不同。

简单来说:

  1. touchesBegantouchesEndedopacity和(b)backgroundCircle图层上的{a} transform更改后应用了CATransform3DRotate } topLayer
  2. CALayer的扩展程序(a)通过addAnimation和(b)添加动画,并通过setValue设置keyPath的值,从而简化代码。
  3. 问题是不透明度不会动画回零,而是逐渐增加,直到完全不透明。但是,当我将keyPath更改为transform以外的任何内容时,它再次有效但是它不会为topLayer 设置动画。我不确定这里究竟是什么错误,但我怀疑它与keyPaths有关。

    以下是方法和扩展:

    1. backgroundCircle相关功能:

      func animateCircleToTransparent(completion:()->()) {
      
      let drawAnimation = CABasicAnimation(keyPath: "opacity")
      drawAnimation.duration = 0.2
      drawAnimation.repeatCount = 1
      drawAnimation.fromValue = backgroundCircleOpacity
      drawAnimation.toValue = 0
      drawAnimation.fillMode = kCAFillModeBackwards
      drawAnimation.autoreverses = false
      drawAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.34, 0.01, 0.69, 1.37)
      self.backgroundCircle.ocb_applyAnimation(drawAnimation)
      
      }
      
      func animateCircleToOpaque(completion:()->()) {
      
      let drawAnimation = CABasicAnimation(keyPath: "opacity")
      drawAnimation.duration = 0.2
      drawAnimation.repeatCount = 1
      drawAnimation.fromValue = 0
      drawAnimation.toValue = backgroundCircleOpacity
      drawAnimation.fillMode = kCAFillModeBackwards
      drawAnimation.autoreverses = false
      drawAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.34, 0.01, 0.69, 1.37)
      self.backgroundCircle.ocb_applyAnimation(drawAnimation)
      }
      
    2. topLayer相关功能:

      func animateTopLineOn() {
      
      let topTransform = CABasicAnimation(keyPath: "transform")
      topTransform.timingFunction = CAMediaTimingFunction(controlPoints: 0.5, -0.8, 0.5, 1.85)
      topTransform.duration = 0.4
      topTransform.fillMode = kCAFillModeBackwards
      
      let translation = CATransform3DMakeTranslation(-4, 0, 0)
      topTransform.toValue = NSValue(CATransform3D: CATransform3DRotate(translation, -0.7853975, 0, 0, 1))
      topTransform.beginTime = CACurrentMediaTime() + 0.25
      
      self.topLayer.ocb_applyAnimation(topTransform)
      }
      
      
      func animateTopLineOff() {
      
      
      let topTransform = CABasicAnimation(keyPath: "transform")
      topTransform.timingFunction = CAMediaTimingFunction(controlPoints: 0.5, -0.8, 0.5, 1.85)
      topTransform.duration = 0.4
      topTransform.fillMode = kCAFillModeBackwards
      
      let translation = CATransform3DMakeTranslation(4, 0, 0)
      topTransform.toValue = NSValue(CATransform3D: CATransform3DRotate(translation, 0.7853975, 0, 0, 1))
      topTransform.beginTime = CACurrentMediaTime() + 0.25
      
      self.topLayer.ocb_applyAnimation(topTransform)
      }
      
    3. '的CALayer'扩展:

      extension CALayer {
      func ocb_applyAnimation(animation: CABasicAnimation) {
      let copy = animation.copy() as! CABasicAnimation
      
      if copy.fromValue == nil {
          copy.fromValue = self.presentationLayer()!.valueForKeyPath(copy.keyPath!)
      }
      
      self.addAnimation(copy, forKey: copy.keyPath!)
      self.setValue(copy.toValue, forKeyPath:copy.keyPath!)
      }
      }
      

0 个答案:

没有答案