Last active
April 14, 2018 15:07
-
-
Save rtking1993/36a0686c5d47af88848994fc075ddedd to your computer and use it in GitHub Desktop.
A view controller that displays CAShapeLayers inside a UIView
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
// MARK: Frameworks | |
import UIKit | |
// MARK: ViewController | |
class ShapeViewController: UIViewController { | |
// MARK: Outlets | |
@IBOutlet var shapeView: UIView! | |
@IBOutlet var buttonView: UIView! | |
@IBOutlet var nextButton: UIButton! | |
@IBOutlet var previousButton: UIButton! | |
// MARK: Variables | |
var shapes: [CAShapeLayer] = [] | |
var index: Int = 0 { | |
didSet { | |
let currentShape = shapes[index] | |
shapeView.layer.sublayers?.removeAll() | |
shapeView.layer.addSublayer(currentShape) | |
} | |
} | |
// MARK: View Methods | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
shapes = [shapeCreator(bezierPath: UIBezierPath(homeIn: shapeView.bounds)), | |
shapeCreator(bezierPath: UIBezierPath(addIn: shapeView.bounds)), | |
shapeCreator(bezierPath: UIBezierPath(profileIn: shapeView.bounds)), | |
shapeCreator(bezierPath: UIBezierPath(searchIn: shapeView.bounds))] | |
index = 0 | |
} | |
// MARK: Helper Methods | |
private func shapeCreator(bezierPath: UIBezierPath) -> CAShapeLayer { | |
let shape = CAShapeLayer() | |
shape.frame = bezierPath.bounds | |
shape.path = bezierPath.cgPath | |
shape.fillColor = UIColor.clear.cgColor | |
shape.strokeColor = UIColor.red.cgColor | |
shape.lineWidth = 2 | |
return shape | |
} | |
// MARK: Action Methods | |
@IBAction func previous(_ sender: Any?) { | |
if index == 0 { | |
index = (shapes.count - 1) | |
} else { | |
index = index - 1 | |
} | |
} | |
@IBAction func next(_ sender: Any?) { | |
if index == (shapes.count - 1) { | |
index = 0 | |
} else { | |
index = index + 1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment