浅谈swift动画(五)
2019-01-04 本文已影响6人
元宝是只小肥猫
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