Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A view controller that displays CAShapeLayers inside a UIView
// 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