Last active
July 24, 2017 02:51
-
-
Save agibson73/60bcf2f97e8eced0381bc7ab9208ee7f to your computer and use it in GitHub Desktop.
CAShapeLayer Test
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
// | |
// PathView.swift | |
// | |
// Created by Alex Gibson on 7/23/17. | |
// Copyright © 2017 AG. All rights reserved. | |
// | |
import UIKit | |
@IBDesignable | |
class PathView: UIView { | |
private var pathShapeLayer = CAShapeLayer() | |
@IBInspectable var shapeBorderColor : UIColor = .gray | |
@IBInspectable var shapeFillColor : UIColor = .white | |
@IBInspectable var shapeBorderWidth : CGFloat = 1.0 | |
@IBInspectable var shouldMaskView : Bool = false | |
private var pathBorderLayer = CAShapeLayer() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
configureLayer() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
configureLayer() | |
} | |
//only used for storyboard | |
override func prepareForInterfaceBuilder() { | |
super.prepareForInterfaceBuilder() | |
configureLayer() | |
} | |
func configureLayer() { | |
//shape is always present | |
let path = createPath(frame: bounds).cgPath | |
pathShapeLayer.frame = bounds | |
pathShapeLayer.path = path | |
pathShapeLayer.lineWidth = shapeBorderWidth | |
pathShapeLayer.strokeColor = shapeBorderColor.cgColor | |
pathShapeLayer.fillColor = shapeFillColor.cgColor | |
//should we mask the view or not? | |
if shouldMaskView == false{ | |
pathShapeLayer.removeFromSuperlayer() | |
layer.addSublayer(pathShapeLayer) | |
}else{ | |
//mask won't show a border | |
pathBorderLayer.frame = bounds | |
pathBorderLayer.path = path | |
pathBorderLayer.lineWidth = shapeBorderWidth | |
pathBorderLayer.strokeColor = shapeBorderColor.cgColor | |
pathBorderLayer.fillColor = shapeFillColor.cgColor | |
layer.mask = pathShapeLayer | |
if let subLs = self.layer.sublayers{ | |
if !subLs.contains(pathBorderLayer){ | |
self.layer.addSublayer(pathBorderLayer) | |
} | |
}else{ | |
self.layer.addSublayer(pathBorderLayer) | |
} | |
} | |
} | |
func createPath(frame: CGRect = CGRect(x: 0, y: 0, width: 395, height: 322))-> UIBezierPath { | |
//// Bezier Drawing | |
let bezierPath = UIBezierPath() | |
bezierPath.move(to: CGPoint(x: frame.minX + 0.05316 * frame.width, y: frame.minY + 0.22050 * frame.height)) | |
bezierPath.addCurve(to: CGPoint(x: frame.minX + 0.31674 * frame.width, y: frame.minY + 0.08517 * frame.height), controlPoint1: CGPoint(x: frame.minX + 0.05316 * frame.width, y: frame.minY + 0.22050 * frame.height), controlPoint2: CGPoint(x: frame.minX + 0.17890 * frame.width, y: frame.minY + -0.00108 * frame.height)) | |
bezierPath.addCurve(to: CGPoint(x: frame.minX + 0.64315 * frame.width, y: frame.minY + 0.40635 * frame.height), controlPoint1: CGPoint(x: frame.minX + 0.45458 * frame.width, y: frame.minY + 0.17143 * frame.height), controlPoint2: CGPoint(x: frame.minX + 0.55204 * frame.width, y: frame.minY + 0.40635 * frame.height)) | |
bezierPath.addCurve(to: CGPoint(x: frame.minX + 0.94684 * frame.width, y: frame.minY + 0.22050 * frame.height), controlPoint1: CGPoint(x: frame.minX + 0.87055 * frame.width, y: frame.minY + 0.40635 * frame.height), controlPoint2: CGPoint(x: frame.minX + 0.94684 * frame.width, y: frame.minY + 0.22050 * frame.height)) | |
bezierPath.addLine(to: CGPoint(x: frame.minX + 0.94684 * frame.width, y: frame.minY + 0.93478 * frame.height)) | |
bezierPath.addLine(to: CGPoint(x: frame.minX + 0.05316 * frame.width, y: frame.minY + 0.93478 * frame.height)) | |
bezierPath.addLine(to: CGPoint(x: frame.minX + 0.05316 * frame.width, y: frame.minY + 0.22050 * frame.height)) | |
bezierPath.close() | |
return bezierPath | |
} | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
configureLayer() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment