美文网首页iOS开发
iOS 动画八:Animation Keys and Deleg

iOS 动画八:Animation Keys and Deleg

作者: _浅墨_ | 来源:发表于2018-07-05 20:44 被阅读63次
    animation delegates

    CAAnimationDelegate 代理方法有两个:

    func animationDidStart(_ anim: CAAnimation) 
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool)
    
    1. 给动画添加key
    flyRight.setValue("form", forKey: "name") 
    flyRight.setValue(heading.layer, forKey: "layer")
    

    这样在 animationDidStop 方法中就可以根据 key 判断和控制某个动画了。

    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" {
                // Note: Remember that value(forKey:) always returns an AnyObject?; therefore you must cast the result to your desired type. Don’t forget that the cast operation can fail, so you must use optionals in the example above to handle error conditions, such as when the name key exists but the layer key does not.
                let layer = anim.value(forKey: "layer") as? CALayer
                // Once that’s done, you set the value of layer to nil to remove the reference to the original layer
                anim.setValue(nil, forKey: "layer")
                
                let pulse = CABasicAnimation(keyPath: "transform.scale")
                pulse.fromValue = 1.25
                pulse.toValue = 1.0
                pulse.duration = 0.25
                // layer? — that means the add(_:forKey:) call will be skipped if there isn’t a layer stored in the animation. And since you set the layer to nil earlier, this pulse animation will only happen the first time the form field flies in from the right.
                layer?.add(pulse, forKey: nil)
            }
    }
    
    2. 组动画 Group Animation
    let groupAnimation = CAAnimationGroup()
        groupAnimation.beginTime = CACurrentMediaTime() + 0.5
        groupAnimation.duration = 0.5
        groupAnimation.fillMode = kCAFillModeBackwards
        groupAnimation.timingFunction = CAMediaTimingFunction( name: kCAMediaTimingFunctionEaseIn)
        
        let scaleDown = CABasicAnimation(keyPath: "transform.scale")
        scaleDown.fromValue = 3.5
        scaleDown.toValue = 1.0
        
        let rotate = CABasicAnimation(keyPath: "transform.rotation")
        rotate.fromValue = .pi / 4.0
        rotate.toValue = 0.0
        
        let fade = CABasicAnimation(keyPath: "opacity")
        fade.fromValue = 0.0
        fade.toValue = 1.0
        
        groupAnimation.animations = [scaleDown, rotate, fade]
        loginButton.layer.add(groupAnimation, forKey: nil)
    

    CAMediaTimingFunction 有几个预设值,我们可以直接使用:

    • kCAMediaTimingFunctionLinear 线性运动
    • kCAMediaTimingFunctionEaseIn 渐进


    • kCAMediaTimingFunctionEaseOut 渐出
    • kCAMediaTimingFunctionEaseInEaseOut 渐进渐出

    demo下载地址

    相关文章

      网友评论

        本文标题:iOS 动画八:Animation Keys and Deleg

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