Skip to content

Instantly share code, notes, and snippets.

@Coder-ACJHP
Created May 3, 2019 12:19
Show Gist options
  • Save Coder-ACJHP/f77dd0b383b9797eff299336f4a5fc5f to your computer and use it in GitHub Desktop.
Save Coder-ACJHP/f77dd0b383b9797eff299336f4a5fc5f to your computer and use it in GitHub Desktop.
Lined circles shrinks and grows with glowing (changing alpha) in order around any view that you specialize.
import UIKit
class TestViewController: UIViewController {
private var timer: Timer?
private var circleShapeCount: Int = 0
private var scaleAnimation: CABasicAnimation? = {
let animation = CABasicAnimation(keyPath: "transform.scale")
animation.fromValue = 0.80
animation.toValue = 2.0
animation.duration = 3.0
animation.isRemovedOnCompletion = false
animation.repeatCount = .infinity
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
return animation
}()
private var opacityAnimation: CABasicAnimation? = {
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = 1.0
animation.toValue = 0
animation.duration = 3.0
animation.isRemovedOnCompletion = false
animation.repeatCount = .infinity
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
return animation
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .cyan
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addPulseAnimation()
}
private func addPulseAnimation() {
timer = Timer.scheduledTimer(timeInterval: 1.2,
target: self,
selector: #selector(createPulse),
userInfo: nil, repeats: true)
timer?.fire()
}
@objc private func createPulse() {
if circleShapeCount < 3 {
circleShapeCount += 1
let circlePath = UIBezierPath(arcCenter: .zero,
radius: 125,
startAngle: 0.0,
endAngle: CGFloat.pi * 2,
clockwise: true)
let circleLayer = CAShapeLayer()
circleLayer.path = circlePath.cgPath
circleLayer.lineWidth = 2
circleLayer.strokeColor = UIColor.appRed.cgColor
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.lineCap = .round
circleLayer.position = view.center
view.layer.addSublayer(circleLayer)
circleLayer.add(scaleAnimation!, forKey: "scaleAnimation")
circleLayer.add(opacityAnimation!, forKey: "opacityAnimation")
} else {
timer?.invalidate()
timer = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment