Skip to content

Instantly share code, notes, and snippets.

@otymartin
Created March 12, 2018 04:19
Show Gist options
  • Save otymartin/32bcb2d9535d30b1204b27224767a8de to your computer and use it in GitHub Desktop.
Save otymartin/32bcb2d9535d30b1204b27224767a8de to your computer and use it in GitHub Desktop.
Playground Timer
//: A UIKit based Playground for presenting user interface
import UIKit
import AVFoundation
import PlaygroundSupport
let arcPoint = CGPoint(x: 170, y: 200)
func BezierPath(center: CGPoint) -> CGPath {
let path = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: CGFloat.pi + CGFloat.pi, clockwise: true)
return path.cgPath
}
class ViewController: UIViewController {
var shapeLayer: CAShapeLayer = {
var shapeLayer = CAShapeLayer()
shapeLayer.path = BezierPath(center: arcPoint)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.white.cgColor
shapeLayer.lineWidth = 20
shapeLayer.lineCap = kCALineCapRound
shapeLayer.strokeEnd = 0
return shapeLayer
}()
var shapeBackground: CAShapeLayer = {
var shapeBackground = CAShapeLayer()
shapeBackground.path = BezierPath(center: arcPoint)
shapeBackground.fillColor = UIColor.clear.cgColor
shapeBackground.strokeColor = UIColor.white.cgColor
shapeBackground.lineWidth = 20
shapeBackground.opacity = 0.3
return shapeBackground
}()
let superView: UIView = {
var view = UIView()
view.frame = CGRect(x: 50, y: 100, width: 350, height: 500)
view.backgroundColor = .black
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.superView)
self.superView.layer.addSublayer(self.shapeBackground)
self.superView.layer.addSublayer(self.shapeLayer)
self.superView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.onTap)))
}
@objc func onTap() {
print("Tapped!")
var basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.fillMode = kCAFillModeForwards
basicAnimation.toValue = 1
basicAnimation.speed = 0.1
basicAnimation.isRemovedOnCompletion = false
self.shapeLayer.add(basicAnimation, forKey: "strokeEnd animation")
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = ViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment