简单的CABasicAnimation动画

作者: 让代码飞 | 来源:发表于2018-06-22 17:30 被阅读93次

实现的简单的动画效果平时仅仅的是用uiview动画就满足了,很少接触到CABasicAnimation,但是最近突然想简单的了解一下

image.png

写了一个长按动画打卡成功的效果,下边是效果图

小动画.gif
       这里是创建的方法
        button.frame = CGRect.init(x: (self.view.frame.size.width-100)/2, y: 100, width: 100, height: 100)
        button.backgroundColor = UIColor.red
        button.layer.cornerRadius = 50
        button.clipsToBounds = true
        button.addTarget(self, action: #selector(TapAction), for: .touchDown)
        button.addTarget(self, action: #selector(touchUp), for: .touchUpInside)
        button.setTitle("下班打卡", for: .normal)
        button.titleLabel?.textAlignment = NSTextAlignment.center
        button.setTitleColor(UIColor.black, for: .normal)
        self.view.addSubview(button)
        
        let cGpath = UIBezierPath.init(ovalIn:button.frame)
        cGpath.fill()
        myLayer.fillColor = UIColor.clear.cgColor
        myLayer.path = cGpath.cgPath
        myLayer.strokeColor = UIColor.clear.cgColor
        myLayer.lineWidth = 8
        myLayer.lineCap = kCALineCapRound
        self.view.layer.addSublayer(myLayer)
//动画结束的代理
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        if flag {
            button.setTitle("打卡成功", for: .normal)
        }else{
            button.setTitle("下班打卡", for: .normal)
        }
    }
    //长按时候触发
    @objc func TapAction () {
        myLayer.strokeColor = UIColor.yellow.cgColor
        let animation = CABasicAnimation.init(keyPath: "strokeEnd")
        animation.fromValue = 0
        animation.toValue = 1
        animation.duration = 2.0
        animation.delegate = self
        myLayer.strokeEnd = 1
        myLayer.add(animation, forKey: "clockOff")
    }
    //点击触发
    @objc func touchUp() {
        if myLayer.animation(forKey: "clockOff") != nil {
            myLayer.strokeColor = UIColor.clear.cgColor
            myLayer.strokeEnd = 0
            myLayer.removeAnimation(forKey: "clockOff")
        }
    }

相关文章

  • iOS知识点总结(6)- 动画

    CABasicAnimation——基本动画 简单的动画效果: CABasicAnimation *moveAni...

  • ios 动画-CoreAnimation geekband

    本次简单说3中动画, 隐式动画CATransaction,显式动画CABasicAnimation and CAK...

  • 简单的CABasicAnimation动画

    实现的简单的动画效果平时仅仅的是用uiview动画就满足了,很少接触到CABasicAnimation,但是最近突...

  • swift中的动画

    尝试用swift写简单动画。CABasicAnimation 和CAKeyframeAnimation 的keyP...

  • Swift-动画效果

    通过设置视图的transform属性实现动画(效率低,简单)CABasicAnimation实现动画(高效)CAK...

  • iOS 动画

    CABasicAnimation 和 CASpringAnimation CABasicAnimation基本动画...

  • 2018-11-28

    1、CABasicAnimation基本动画 各种属性 CABasicAnimation基本动画 各种属性 - u...

  • iOS之核心动画

    简单介绍两个比较常用的动画类一、基础动画(CABasicAnimation)属性解析:fromValue: key...

  • 关于CABasicAnimation的属性解析

    做了很多动画,明明记得以前写过动画相关的博客,现在找不到了。重新写个简单的吧。 CABasicAnimation ...

  • 动画学习一——CABasicAnimation

    一、动画属性 对于CABasicAnimation动画的使用,相对来说比较简单,我们先来看一下动画的基本属性: 二...

网友评论

本文标题:简单的CABasicAnimation动画

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