简单的CABasicAnimation动画

实现的简单的动画效果平时仅仅的是用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中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,531评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,717评论 6 30
  • 1 CALayer IOS SDK详解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi阅读 10,586评论 3 23
  • 中午吃完饭同事拿来了一串葡萄让我品尝,我笑笑说自己已经吃饱了,吃不下东西了,同事却悠悠的又劝我道:“你还是尝几颗吧...
    宋唐风月阅读 5,202评论 2 2
  • 昨日下午突然问起身边的同事:单单,你和前任是为什么分手的。 已经一年多不曾讨论过情感的问题,这么问的时候自己也很诧...
    消失的书店阅读 3,345评论 0 0