Skip to content

Instantly share code, notes, and snippets.

@churabou
Last active July 13, 2019 13:09
Show Gist options
  • Save churabou/bcc3218df36018d345364eb5c18e537a to your computer and use it in GitHub Desktop.
Save churabou/bcc3218df36018d345364eb5c18e537a to your computer and use it in GitHub Desktop.
Anim
final class AnimationView: UIView {
private let shapeLayer: CAShapeLayer = {
let it = CAShapeLayer()
it.fillColor = UIColor.clear.cgColor
it.strokeColor = UIColor(hex: 0xFF598B).cgColor
it.lineWidth = 5
it.lineCap = .round
it.strokeEnd = 0
return it
}()
private let trackLayer: CAShapeLayer = {
let it = CAShapeLayer()
it.fillColor = UIColor.clear.cgColor
it.strokeColor = UIColor(hex: 0xDDDDDD).cgColor
it.lineWidth = 5
it.lineCap = .round
return it
}()
override func draw(_ rect: CGRect) {
let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
let circlePath = UIBezierPath(arcCenter: center,
radius: 100,
startAngle: -.pi / 2,
endAngle: .pi * 3 / 2,
clockwise: true)
[trackLayer, shapeLayer].forEach { layer in
layer.path = circlePath.cgPath
self.layer.addSublayer(layer)
}
}
func startAnimation() {
let anim = CABasicAnimation(keyPath: "strokeEnd")
anim.fromValue = 0
anim.toValue = 1
anim.duration = 1
anim.isRemovedOnCompletion = false
anim.fillMode = .forwards
shapeLayer.add(anim, forKey: "key")
}
}
final class AnimationView: UIView {
private lazy var shapeLayer: CAShapeLayer = {
let it = CAShapeLayer()
it.lineWidth = 3
it.strokeColor = UIColor(hex: 0xEFEFEF).cgColor
it.fillColor = UIColor.clear.cgColor
it.path = path.cgPath
return it
}()
private let circleLayer: CALayer = {
let it = CALayer()
it.backgroundColor = UIColor(hex: 0xFDCB6E).cgColor
it.bounds.size.width = 20
it.bounds.size.height = 20
it.cornerRadius = 10
return it
}()
private lazy var path: UIBezierPath = {
let path = UIBezierPath()
let movepoints: [CGPoint] = [
.init(x: bounds.minX + 100, y: 200),
.init(x: bounds.midX, y: 100),
.init(x: bounds.maxX - 100, y: 200)
]
movepoints.enumerated().forEach { index, point in
index == 0 ? path.move(to: point) : path.addLine(to: point)
}
return path
}()
override func draw(_ rect: CGRect) {
shapeLayer.frame = rect
layer.addSublayer(shapeLayer)
layer.addSublayer(circleLayer)
}
func startAnimation() {
let anim = CAKeyframeAnimation(keyPath: "position")
anim.duration = 2
anim.isRemovedOnCompletion = false
anim.fillMode = .forwards
anim.path = path.cgPath
circleLayer.add(anim, forKey: nil)
}
}
extension UIColor {
convenience init(hex: UInt32) {
let red = CGFloat((hex & 0xFF0000) >> 16) / 255.0
let green = CGFloat((hex & 0x00FF00) >> 8) / 255.0
let blue = CGFloat(hex & 0x0000FF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment