Created
April 15, 2021 13:08
-
-
Save smosko/816bb4cf6cf384631d8b440846fe07b1 to your computer and use it in GitHub Desktop.
Radar animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class RadarView: UIView { | |
private let waveShape = CAShapeLayer() | |
private let waveReplicator = CAReplicatorLayer() | |
override init(frame: CGRect = .zero) { | |
super.init(frame: frame) | |
waveShape.fillColor = UIColor.orange.cgColor | |
waveShape.opacity = 0 | |
let scaleAnimation = CABasicAnimation(keyPath: "transform") | |
scaleAnimation.valueFunction = CAValueFunction(name: .scale) | |
scaleAnimation.fromValue = [0, 0, 0] | |
scaleAnimation.toValue = [2, 2, 0] | |
let fadeAnimation = CABasicAnimation(keyPath: "opacity") | |
fadeAnimation.fromValue = 0.6 | |
fadeAnimation.toValue = 0 | |
let waveAnimation = CAAnimationGroup() | |
waveAnimation.animations = [scaleAnimation, fadeAnimation] | |
waveAnimation.duration = 6 | |
waveAnimation.repeatCount = HUGE | |
waveShape.add(waveAnimation, forKey: nil) | |
waveReplicator.addSublayer(waveShape) | |
waveReplicator.instanceCount = 4 | |
waveReplicator.instanceDelay = 1.5 | |
layer.addSublayer(waveReplicator) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
waveShape.frame = bounds | |
waveShape.path = UIBezierPath(ovalIn: bounds).cgPath | |
waveReplicator.frame = bounds | |
} | |
} | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let radarView = RadarView() | |
radarView.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(radarView) | |
NSLayoutConstraint.activate([ | |
radarView.centerXAnchor.constraint(equalTo: view.centerXAnchor), | |
radarView.centerYAnchor.constraint(equalTo: view.bottomAnchor, constant: -32), | |
radarView.widthAnchor.constraint(equalTo: view.widthAnchor), | |
radarView.heightAnchor.constraint(equalTo: radarView.widthAnchor) | |
]) | |
} | |
} |
Author
smosko
commented
Apr 15, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment