You asked:
- How can latency latency during the continuous painting period be eliminated?
As you correctly understood, yes, by taking a picture and dropping the path, you can fix this by limiting how long the path will be.
I know that you know about it, but for other readers, in iOS 9, you can also use predictive touches. In this particular algorithm (where (a) you just add a path, but (b) every fourth point is adjusted based on the next point to make sure there are no gaps where the two cubic bezel-less curves connect), which is a bit more complicated, but can be done .
- How can you eliminate the mismatch in the drawn paths?
This is because the snapshot includes a time path. But the goal of this time path is that it will be discarded as more points arrive. Therefore, you should not include it in the snapshot that you create in the middle of gestures.
So, I would suggest adding a parameter to the snapshot function, which indicates whether to include temporaryPath or not. When you call it in the middle of gestures, you must specify includeTemporaryPath as false , but when called at the end of the gesture, includeTemporaryPath will be true .
For instance:
class SmoothCurvedLinesView: UIView { var strokeColor = UIColor.blueColor() var lineWidth: CGFloat = 20 var snapshotImage: UIImage? private var path: UIBezierPath? private var temporaryPath: UIBezierPath? private var points = [CGPoint]() private var totalPointCount = 0 override func drawRect(rect: CGRect) { snapshotImage?.drawInRect(rect) strokeColor.setStroke() path?.stroke() temporaryPath?.stroke() } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch: AnyObject? = touches.first points = [touch!.locationInView(self)] totalPointCount = totalPointCount + 1 } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch: AnyObject? = touches.first let point = touch!.locationInView(self) points.append(point) totalPointCount = totalPointCount + 1 updatePaths() if totalPointCount > 50 { constructIncrementalImage(includeTemporaryPath: false) path = nil totalPointCount = 0 } setNeedsDisplay() } private func updatePaths() {
By the way, while I was the one who provided the code for generating the code, I realized that it could be simplified. I also fixed the error. See code above.
Then you asked:
- How to change alpha / opacity of a path and temporary table?
You can simply adjust the color with which you call setStroke with the corresponding alpha. For example, if you want the temporary path to be in half alpha main path, you could do something like:
override func drawRect(rect: CGRect) { snapshotImage?.drawInRect(rect) strokeColor.setStroke() path?.stroke() strokeColor.colorWithAlphaComponent(0.5).setStroke() temporaryPath?.stroke() }