Last active
June 19, 2016 20:36
-
-
Save joshheald/14ff4d9e2d19e911d6676a9a1d462317 to your computer and use it in GitHub Desktop.
A simple circle drawing/erasing view
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 ViewController: UIViewController { | |
lazy var circleLayer: CAShapeLayer = { | |
let layer = CAShapeLayer() | |
layer.frame = self.circleView.bounds | |
let bezierPath = UIBezierPath(ovalInRect: layer.bounds) | |
self.rotate(bezierPath: bezierPath, degrees: -90.0) | |
layer.path = bezierPath.CGPath | |
layer.lineWidth = 4.0 | |
layer.strokeColor = UIColor.whiteColor().CGColor | |
layer.fillColor = UIColor.clearColor().CGColor | |
layer.strokeStart = 0 | |
self.circleView.layer.addSublayer(layer) | |
return layer | |
}() | |
@IBOutlet weak var circleView: UIView! | |
private func rotate(bezierPath path: UIBezierPath, degrees: CGFloat) { | |
let box = CGPathGetPathBoundingBox(path.CGPath) | |
let translate = CGAffineTransformMakeTranslation(-1 * (box.origin.x + (box.size.width / 2)), -1 * (box.origin.y + (box.size.height / 2))) | |
path.applyTransform(translate) | |
let rotate = CGAffineTransformMakeRotation(degrees * CGFloat(M_PI) / 180) | |
path.applyTransform(rotate) | |
let translateBack = CGAffineTransformMakeTranslation((box.origin.x + (box.size.width / 2)), (box.origin.y + (box.size.height / 2))) | |
path.applyTransform(translateBack) | |
} | |
private func drawCircle(from fromValue: CGFloat, to toValue: CGFloat) { | |
circleLayer.strokeEnd = toValue | |
let end = CABasicAnimation(keyPath: "strokeEnd") | |
end.fromValue = fromValue | |
end.toValue = toValue | |
end.duration = 0.2 | |
circleLayer.addAnimation(end, forKey: nil) | |
} | |
@IBAction func reverseTapped(sender: AnyObject) { | |
drawCircle(from: 1, to: 0) | |
} | |
@IBAction func forwardTapped(sender: AnyObject) { | |
drawCircle(from: 0, to: 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment