Created
December 22, 2016 17:39
-
-
Save sergii-frost/03096f2d1c78cea894e3a5b746be3378 to your computer and use it in GitHub Desktop.
Pulse View playground
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import PlaygroundSupport | |
let kStartAlpha: CGFloat = 0.8 | |
let kEndAlpha: CGFloat = 0.0 | |
let kDuration: CFTimeInterval = 1.0 | |
let bgColor = UIColor(red: 153.0/255, green: 192.0/255, blue: 204.0/255, alpha: 1.0) | |
let pulseColor = UIColor.white.withAlphaComponent(kStartAlpha) | |
class View: UIView { | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
if let sublayers = self.layer.sublayers { | |
sublayers.forEach({ (sublayer) in | |
sublayer.removeFromSuperlayer() | |
}) | |
} | |
let center = self.center | |
let minRadius = min(center.x, center.y) / 3.0 | |
let maxRadius = min(center.x, center.y) / 1.5 | |
let innerCircleLayer = drawCircle(center: center, radis: minRadius, color: pulseColor) | |
let outerCircleLayer = drawCircle(center: center, radis: minRadius, color: pulseColor) | |
self.layer.addSublayer(outerCircleLayer) | |
self.layer.addSublayer(innerCircleLayer) | |
addAnimation(to: outerCircleLayer, center: center, startRadius: minRadius, endRadius: maxRadius) | |
} | |
func drawCircle(center: CGPoint, radis: CGFloat, color: UIColor) -> CAShapeLayer{ | |
let circleLayer = CAShapeLayer() | |
circleLayer.path = getCirclePath(center: center, radius: radis).cgPath | |
circleLayer.fillColor = color.cgColor | |
circleLayer.lineWidth = 0.0 | |
return circleLayer | |
} | |
private func getCirclePath(center: CGPoint, radius: CGFloat) -> UIBezierPath { | |
return UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(0), endAngle: CGFloat(M_PI*2), clockwise: true) | |
} | |
private func addAnimation(to layer: CAShapeLayer, center: CGPoint, startRadius: CGFloat, endRadius: CGFloat, startOpacity: CGFloat = kStartAlpha, endOpacity: CGFloat = kEndAlpha, duration: CFTimeInterval = kDuration) { | |
let startCirclePath = getCirclePath(center: center, radius: startRadius) | |
let endCirclePath = getCirclePath(center: center, radius: endRadius) | |
let radiusAnimation = CABasicAnimation(keyPath: "path") | |
radiusAnimation.fromValue = startCirclePath.cgPath | |
radiusAnimation.toValue = endCirclePath.cgPath | |
let opacityAnimation = CABasicAnimation(keyPath: "opacity") | |
opacityAnimation.fromValue = startOpacity | |
opacityAnimation.toValue = endOpacity | |
let group = CAAnimationGroup() | |
group.animations = [radiusAnimation, opacityAnimation] | |
group.duration = kDuration | |
group.repeatCount = Float(CGFloat.greatestFiniteMagnitude) | |
group.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) | |
group.isRemovedOnCompletion = false | |
layer.add(group, forKey: "opacityAndRadius") | |
} | |
} | |
let myAwesomeView = View(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) | |
myAwesomeView.backgroundColor = bgColor | |
PlaygroundPage.current.liveView = myAwesomeView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment