Last active
June 28, 2020 02:41
-
-
Save erica/5cee50674304915c66d6 to your computer and use it in GitHub Desktop.
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 | |
extension UIBezierPath { | |
func traverseElements() -> NSArray { | |
var array: [AnyObject] = [] | |
withUnsafeMutablePointer(&array) { | |
arrayPtr -> Void in | |
CGPathApply(CGPath, UnsafeMutablePointer(arrayPtr), { | |
(arrayPointer: UnsafeMutablePointer<Void>, | |
elementPointer: UnsafePointer<CGPathElement>) -> Void in | |
var array = UnsafeMutablePointer<[AnyObject]>(arrayPointer).memory | |
let element = unsafeBitCast(elementPointer.memory, CGPathElement.self) | |
// Populate the array with something, print out stuff during traversal | |
switch element.type { | |
case .AddCurveToPoint: | |
print("Add Curve to Point", element.points[0], element.points[1], element.points[2]) | |
array.append("Curve") | |
case .AddLineToPoint: | |
print("Add Line to Point", element.points[0]) | |
array.append("line") | |
case .AddQuadCurveToPoint: | |
print("Add Quad to Point", element.points[0], element.points[1]) | |
array.append("quad") | |
case .CloseSubpath: | |
print("Close subpath") | |
array.append("close") | |
case .MoveToPoint: | |
print("move to point", element.points[0]) | |
array.append("move") | |
} | |
}) | |
} | |
return array | |
} | |
} | |
let path = UIBezierPath(roundedRect: CGRectMake(0, 0, 50, 20), cornerRadius: 4) | |
let array = path.traverseElements() | |
print(array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment