15.CABasicAnimation 单个View展开动画和

2017-06-22  本文已影响0人  小笨憨

展开动画

        self.layer.anchorPoint  = CGPoint.init(x: 0.5, y: 0)
        let baseAnimation = CABasicAnimation.init(keyPath: "transform")
        
        baseAnimation.fromValue = NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 0.01, 0.01))
        baseAnimation.toValue   = NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0))
        baseAnimation.beginTime = CACurrentMediaTime()
        baseAnimation.duration  = 0.5
        baseAnimation.autoreverses      = false // 动画执行逆动画
        baseAnimation.timingFunction    = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
        baseAnimation.isRemovedOnCompletion = true // 完成后移除动画 想要在动画完成后的delegate
        做操作的话就改成false 配合动画执行完不返回原状态效果更好
        baseAnimation.fillMode          = kCAFillModeForwards // 动画执行完后不返回原状态
        baseAnimation.delegate          = self as? CAAnimationDelegate
        baseAnimation.setValue("addAnimation", forKey: "animType")

        self.layer.add(baseAnimation, forKey: "transform")

收起

 func createRemoveAnimationFromeTop() -> Void {
        
        self.layer.anchorPoint  = CGPoint.init(x: 0.5, y: 0)
        let transform = CABasicAnimation.init(keyPath: "transform")
        
        transform.fromValue     = NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0))
        transform.toValue       = NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 0.01, 0.01))
        transform.duration      = 0.5
        transform.beginTime     = CACurrentMediaTime()
        transform.timingFunction    = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
        transform.delegate      = self as? CAAnimationDelegate
        transform.isRemovedOnCompletion = false
        transform.fillMode              = kCAFillModeForwards
        transform.setValue("removeAnimation", forKey: "animType")
        self.layer.add(transform, forKey: "removeAnimation")
    }
    // UIView执行动画   所有拥有这个key的动画的View都会执行
    override func removeFromSuperview() {
        
        self.layer.removeAllAnimations()
        self.createRemoveAnimationFromeTop()
        UIView.beginAnimations("removeAnimation", context: nil)
    }
 func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        
        if flag {
            
            let value = anim.value(forKey: "animType") as! NSString

            if value.isEqual(to: "removeAnimation") {
            
                // 动画执行完毕,关掉beginAnimation  防止一些不
                // 想要的情况出现
                UIView.commitAnimations()
                super.removeFromSuperview()
            }
        }
    }

动画暂停

func pauseLayer(layer:CALayer) -> Void {
        
        let pauseTimed = layer.convertTime(CACurrentMediaTime(), from: nil)
        layer.speed  = 0
        layer.timeOffset = pauseTimed
    }

动画继续

func resumeLayer(layer:CALayer) -> Void {
        
        let pausedTime = layer.timeOffset
        layer.speed         = 1.0
        layer.timeOffset    = 0.0
        layer.beginTime     = 0.0
        let timeSincePause  = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
        layer.beginTime     = timeSincePause
    }
上一篇下一篇

猜你喜欢

热点阅读