Skip to content

Instantly share code, notes, and snippets.

@erica
Created March 29, 2015 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/d74ea0bcd24b4e99a7ed to your computer and use it in GitHub Desktop.
Save erica/d74ea0bcd24b4e99a7ed to your computer and use it in GitHub Desktop.
Before and After
public extension UIBezierPath {
public var dashPhase : CGFloat? {
get {
// Only defined if dashes are defined
if dashes.count == 0 {return nil}
var phase = UnsafeMutablePointer<CGFloat>.alloc(1)
getLineDash(nil, count: nil, phase: phase)
let result = phase.memory; free(phase)
return result
}
set {
if dashes.count == 0 {
println("Error: attempting to set dash phase without existing dash pattern")
return
}
var newPhase : CGFloat = newValue == nil ? 0.0 : newValue!
setDashes(phase: newPhase, dashes: dashes)
}
}
private func setDashes(#phase: CGFloat, dashes: [CGFloat]) {
var pattern = UnsafeMutablePointer<CGFloat>.alloc(dashes.count)
for (index, dash) in enumerate(dashes) {pattern[index] = CGFloat(dash)}
setLineDash(pattern, count: dashes.count, phase: phase)
free(pattern)
}
private func setDashes_(dashes: [CGFloat]) {
var phase : CGFloat = dashPhase == nil ? 0.0 : dashPhase!
setDashes(phase: phase, dashes: dashes)
}
public func setDashes(dashes: [Double]) {
var phase : CGFloat = dashPhase == nil ? 0.0 : dashPhase!
setDashes(phase: phase, dashes: dashes.map{CGFloat($0)})
}
public func setDashes(dashes: Double...) {setDashes(dashes)}
public var dashes : [CGFloat] {
var count = UnsafeMutablePointer<Int>.alloc(1)
getLineDash(nil, count: count, phase: nil)
var buffer = UnsafeMutablePointer<CGFloat>.alloc(count.memory)
getLineDash(buffer, count: nil, phase: nil)
var result = [CGFloat]()
for index in 0..<count.memory {result += [buffer[index]]}
free(buffer); free(count)
return result
}
}
public extension UIBezierPath {
var dashCount : Int {
var dashCount_ : Int = 0
getLineDash(nil, count: &dashCount_, phase: nil)
return dashCount_
}
var dashPhase : CGFloat {
get {
if dashCount == 0 {return 0.0}
var dashPhase_ : CGFloat = 0.0
getLineDash(nil, count: nil, phase: &dashPhase_)
return dashPhase_
}
set {
if dashCount == 0 {return}
var dashes_ = dashes
setLineDash(&dashes_, count: count(dashes_), phase: newValue)
}
}
var dashes : [CGFloat] {
get {
if dashCount == 0 {return [CGFloat]()}
var dashes_ = [CGFloat](count:dashCount, repeatedValue: 0.0)
getLineDash(&dashes_, count: nil, phase: nil)
return dashes_
}
set {
var newDashes = newValue
setLineDash(&newDashes, count: count(newDashes), phase: 0.0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment