codingmeswiftly / uibezierpath-superpowers Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
I'm using mx_perpendicularPoint so when a user taps on the screen we can highlight the nearest "drawing object".
Sometimes a line segment might not have a length (i.e. user touched/released to make a dot), but it is visible because of setLineCapStyle:kCGLineCapRound.
In calculatePointLookupTable it doesn't take into account a single point "path" like this, element.length is zero and points.append(element.point(at: offset / element.length)) fails because offset is divided by zero producing Nan for an index.
Thanks for writing this! I want almost this exact thing, except for NSBezierPath (I'm not writing a iOS app, I'm driving a laser cutter from my Mac).
I'm going to give writing the NSBezierPath version a shot.
Been using the code no problems for days. Sent out a beta and it crashes for everyone. I don't know why, but it seems that when it gets to this point it crashes:
block?(element.pointee)
I could only get it to crash doing a release build and I'm not proficient enough at swift to understand why it's failing.
Searching around for ideas/code I ran across this devforum post (notably Eskimo's):
https://forums.developer.apple.com/message/15725#15725
I used this code he posted:
typealias CGPathApplierBlock = @convention(block) (CGPathElement) -> Void
extension CGPath {
func apply(body: CGPathApplierBlock) {
self.apply(info: unsafeBitCast(body, to: UnsafeMutableRawPointer.self)) { (info, element) in
let block2 = unsafeBitCast(info, to: CGPathApplierBlock.self)
block2(element.pointee)
}
}
}
And now instead of:
enumeratePathElements { element in
I use:
cgPath.apply { element in
And it works without crashing in debug and release build. I'm building with xcode 9.1 and most testing on iOS 11 devices.
It is possible to build a UIBezierPath
consisting of "points" rather than actual line elements. In that case point is a zero length line element.
Example:
let p: CGPoint = ...
path.move(to: p)
path.addLine(to: p)
The current implementation does not account for this possibility.
Most notably, findPathElement(at:, callback:)
, which is basically the spine of the library, will search for path elements based on their lengthRange
. Things start to burn all over the place when this range is 0...0
, which it is for "points".
See #1
calculatePointLookupTable()
should never yield an empty array for a path element. However, this can currently happen when the element has zero (or in fact a very small) length.
See #1
Please update this code to your file, your code is old and will not run,
fileprivate extension CGPath {
func apply(closure: CGPathApplierClosure) {
self.apply(info: unsafeBitCast(closure, to: UnsafeMutableRawPointer.self)) { (info, element) in
let block = unsafeBitCast(info, to: CGPathApplierClosure.self)
block(element.pointee)
}
}
}
replace above function with
fileprivate extension CGPath {
func apply(closure: CGPathApplierClosure) {
withoutActuallyEscaping(closure) { escapedClosure in
self.apply(info: unsafeBitCast(escapedClosure, to: UnsafeMutableRawPointer.self)) { (info, element) in
let block = unsafeBitCast(info, to: CGPathApplierClosure.self)
block(element.pointee)
}
}
}
}
also, there are few warnings, please check and update it, if possible give cocoa pods support for this feature.
i want to thank you for creating this file, this helps me so much that i can not able to get any kind of solution on internet.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.