I need to implement a lasso-like function on a UIView, the place a path is recorded because the finger strikes. When the finger is lifted, the realm is closed and displayed as a inexperienced closed area on the UIView. Then, a UIImage is generated the place the closed space is white and the remainder of the realm is black.
When there are intersections, shut the realm based mostly on the intersection factors.
If there are not any intersections, mechanically join the final level with the primary level to shut the realm.
just like the gif under
Can somebody present an instance of tips on how to implement this? Please use Swift on iOS
import UIKit
class DrawingAreaView: UIView {
non-public var path = UIBezierPath()
non-public var startPoint: CGPoint?
non-public var isPathClosed = false
override func draw(_ rect: CGRect) {
UIColor.clear.setFill()
UIRectFill(rect)
UIColor.black.setStroke()
path.stroke()
if isPathClosed {
UIColor.white.setFill()
path.fill()
}
}
override func touchesBegan(_ touches: Set<UITouch>, with occasion: UIEvent?) {
guard let contact = touches.first else { return }
startPoint = contact.location(in: self)
path.transfer(to: startPoint!)
isPathClosed = false
}
override func touchesMoved(_ touches: Set<UITouch>, with occasion: UIEvent?) {
guard let contact = touches.first, let startPoint = startPoint else { return }
let currentPoint = contact.location(in: self)
path.addLine(to: currentPoint)
self.setNeedsDisplay()
}
override func touchesEnded(_ touches: Set<UITouch>, with occasion: UIEvent?) {
guard let startPoint = startPoint else { return }
// The way to get the realm and detecting Intersections, shut path
self.setNeedsDisplay()
let maskImage = createMaskImage()
}
non-public func createMaskImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.measurement, false, 0)
if let context = UIGraphicsGetCurrentContext() {
UIColor.black.setFill()
context.fill(self.bounds)
UIColor.white.setFill()
path.fill()
}
let picture = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return picture
}
}