浅谈swift动画(五)

AnimationButton

//
//  AnimationButton.swift
//  AnimationBegin
//
//  Created by 刘二拐 on 2018/12/13.
//  Copyright © 2018 jones. All rights reserved.
//

import UIKit

class AnimationButton: UIView,CAAnimationDelegate {
    var button_x: CGFloat = 0
    var button_y: CGFloat = 0
    var button_w: CGFloat = 0
    var button_h: CGFloat = 0
    var view: UIView?
    var viewborder:UIView?
    var label: UILabel?
    var circleView: CustomCircleView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        button_x = frame.origin.x
        button_y = frame.origin.y
        button_w = frame.size.width
        button_h = frame.size.height
        view = UIView(frame:CGRect(x: 0, y: 0, width: button_w, height: button_h))
        view!.backgroundColor = .red
        addSubview(view!)
        viewborder = UIView(frame:CGRect(x: 0, y: 0, width: button_w, height: button_h))
        viewborder!.backgroundColor = UIColor.clear
        viewborder!.layer.borderColor = UIColor.black.cgColor
        viewborder!.layer.borderWidth = 3.0
        addSubview(viewborder!)
        
        label = UILabel(frame:CGRect(x: 0, y: 0, width: button_w, height: button_h))
        label!.text = "UpLoad"
        label!.textAlignment = NSTextAlignment.center
        label!.textColor = UIColor.white
        label!.font = UIFont.systemFont(ofSize: 20.0)
        addSubview(label!)
        
        circleView = CustomCircleView(frame:CGRect(x: 0, y: 0, width: button_w, height: button_h))
        circleView!.delegate = self
        addSubview(circleView!)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func startCustomAnimation() {
        label?.removeFromSuperview()
        let basicRadius = CABasicAnimation()
        basicRadius.keyPath = "cornerRadius"
//        basicRadius.fromValue = 15
        basicRadius.toValue = button_h / 2.0
        
        let basicBounds = CABasicAnimation()
        basicBounds.keyPath = "bounds"
        basicBounds.toValue = NSValue(cgRect:CGRect(x: 0 , y: 0, width: button_h, height: button_h))
        
        let basicAlpha = CABasicAnimation()
        basicAlpha.keyPath = "opacity"
        basicAlpha.toValue = 0
        
        let group = CAAnimationGroup()
        group.duration = 1
        group.repeatCount = 1
        group.isRemovedOnCompletion = false
        group.fillMode = kCAFillModeForwards
        group.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        group.animations = [basicRadius,basicBounds,basicAlpha]
        
        let basicBorderColor = CABasicAnimation()
        basicBorderColor.keyPath = "borderColor"
        basicBorderColor.toValue = UIColor.blue.cgColor
        
        let groupAll = CAAnimationGroup()
        groupAll.duration = 1
        groupAll.repeatCount = 1
        groupAll.isRemovedOnCompletion = false
        groupAll.fillMode = kCAFillModeForwards
        groupAll.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        groupAll.animations = [basicRadius,basicBounds,basicBorderColor]
        groupAll.delegate = self
        groupAll.setValue("groupAllValue", forKey: "groupAllKey")
        CATransaction.begin()
        view!.layer.add(group, forKey: nil)
        viewborder!.layer.add(groupAll, forKey: nil)
        CATransaction.commit()
    }
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        guard flag == true else {
            return
        }
        if (anim.value(forKey: "groupAllKey") != nil) {
            circleView?.strokeChart()
        } else if (anim.value(forKey: "groupAllKey_") != nil) {
            label = UILabel(frame:CGRect(x: 0, y: 0, width: button_w, height: button_h))
            label!.text = "Complete"
            label!.textAlignment = NSTextAlignment.center
            label!.textColor = UIColor.white
            label!.font = UIFont.systemFont(ofSize: 20)
            addSubview(label!)
        }
    }
    
}

// MARK: - CustomCircleDelegate
extension AnimationButton: CustomCircleDelegate {
    func customCircleAnimationStop() {
        circleView?.removeFromSuperview()
        startAnimationSpread()
    }
    func startAnimationSpread() {
        let basicRadius = CABasicAnimation()
        basicRadius.keyPath = "cornerRadius"
        basicRadius.fromValue = button_h/2.0
        basicRadius.toValue = 0
        
        let basicBounds = CABasicAnimation()
        basicBounds.keyPath = "bounds"
        basicBounds.fromValue = NSValue(cgRect:CGRect(x: button_x + (button_w - button_h)/2, y: button_h, width: button_h, height: button_h))
        basicBounds.toValue = NSValue(cgRect:CGRect(x: 0, y: 0, width: button_w, height: button_h))
        
        let basicAlpha:CABasicAnimation = CABasicAnimation()
        basicAlpha.keyPath = "opacity";
        basicAlpha.toValue = 1.0
        
        let basicBackgroundColor:CABasicAnimation = CABasicAnimation()
        basicBackgroundColor.keyPath = "backgroundColor"
        basicBackgroundColor.toValue = UIColor.red.cgColor
        
        let group = CAAnimationGroup()
        group.duration = 1
        group.repeatCount = 1
        group.isRemovedOnCompletion = false
        group.fillMode = kCAFillModeForwards
        group.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        group.animations = [basicRadius, basicBounds, basicAlpha, basicBackgroundColor]
        
        let basicBorder = CABasicAnimation()
        basicBorder.keyPath = "borderColor"
        basicBorder.toValue = UIColor.red.cgColor
        let allGroup = CAAnimationGroup()
        allGroup.duration = 1
        allGroup.repeatCount = 1
        allGroup.isRemovedOnCompletion = false
        allGroup.fillMode=kCAFillModeForwards
        allGroup.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        allGroup.animations = [basicRadius, basicBounds, basicAlpha, basicBorder]
        CATransaction.begin()
        view?.layer.add(group, forKey: nil)
        allGroup.setValue("groupAllValue_", forKey: "groupAllKey_")
        allGroup.delegate = self
        viewborder?.layer.add(allGroup, forKey: nil)
        CATransaction.commit()
    }
    
}
protocol CustomCircleDelegate {
    func  customCircleAnimationStop()
}
class CustomCircleView: UIView,CAAnimationDelegate {
    var lineWidth: CGFloat = 3.0
    var strokeColor = UIColor.purple
    var circle = CAShapeLayer()
    var delegate:CustomCircleDelegate?
    var isC: Bool = false
    override init(frame: CGRect) {
        super.init(frame: frame)
        let startAngle:CGFloat = -90.0/180.0 * CGFloat.pi
        let endAngle:CGFloat = -90.01/180.0 * CGFloat.pi
        let circlePath:UIBezierPath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2,y: frame.size.height/2), radius: frame.size.height/2-2, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        circle.path = circlePath.cgPath
        circle.lineCap = kCALineCapRound
        circle.fillColor = UIColor.clear.cgColor
        circle.lineWidth = CGFloat(lineWidth)
        
        self.layer.addSublayer(circle)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func strokeChart() {
        circle.lineWidth = lineWidth
        circle.strokeColor = strokeColor.cgColor
        let pathAnimation = CABasicAnimation()
        pathAnimation.keyPath = "strokeEnd"
        pathAnimation.delegate = self
        pathAnimation.duration = 3.0
        pathAnimation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseInEaseOut)
        pathAnimation.fromValue = 0.0
        pathAnimation.toValue = 1.0
        pathAnimation .setValue("circle", forKey: "pathAnimation")
        circle.add(pathAnimation, forKey: "strokeEndAnimationcircle")
    }
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
             delegate!.customCircleAnimationStop()
    }
}

调用

import UIKit

class ViewController: UIViewController {
    let startAngle:CGFloat = -90.0/180.0 * CGFloat.pi
    let endAngle:CGFloat = -90.01/180.0 * CGFloat.pi
    var singleTap:UITapGestureRecognizer?
    var buttonview:AnimationButton?
    override func viewDidLoad() {
        super.viewDidLoad()
        buttonview = AnimationButton(frame:CGRect(x: 100, y: 400, width: 210, height: 70))
        singleTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.viewAction))
        buttonview?.addGestureRecognizer(singleTap!)
        self.view.addSubview(buttonview!)
    }
    func viewAction(){
        buttonview!.startCustomAnimation()
    }
}
image.png
image.png
image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,820评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,648评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,324评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,714评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,724评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,328评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,897评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,804评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,345评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,431评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,561评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,238评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,928评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,417评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,528评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,983评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,573评论 2 359

推荐阅读更多精彩内容