I'm encountering an issue with my drawing functionality in Swift, specifically when using CALayer instead of UIBezierPath. Here's the problem I'm facing:
I am creating a texture from an image and using the texture to create a drawing line through finger stroke. In this process, the line is being created by joining the texture one by one, side by side. Initially, I used UIBezierPath for drawing, and everything worked fine. However, when I switched to using CALayer for drawing, for using textures to draw lines, I noticed that the touch points are not sequentially joining or increasing if I move my finger fast.

When I move my finger slowly, the drawing works fine, and the touch points are joining sequentially. However, when I move my finger fast, the touch points seem to skip, resulting in disjointed lines and multiple blank spaces within a line.
class CustomStrokeDrawingView: UIView {
var path = UIBezierPath()
var startPoint = CGPoint()
var touchPoint = CGPoint()
var shape = UIImage(named:"square-outline")
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let point = touch?.location(in: self) {
startPoint = point
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
if let point = touch?.location(in: self) {
touchPoint = point
}
let imageLayer = CALayer()
imageLayer.backgroundColor = UIColor.clear.cgColor
imageLayer.bounds = CGRect(x: startPoint.x, y: startPoint.y , width: 20, height: 20)
imageLayer.position = CGPoint(x:touchPoint.x ,y:touchPoint.y)
imageLayer.contents = shape?.cgImage
self.layer.addSublayer(imageLayer)
}
override func draw(_ rect: CGRect) {
super.draw(rect)
}
}
It is very easy to move your finger faster than iOS can generate
touchesMovedevents ... if you log the points withprint()statements, you can easily see this in the debug console. It's possible to "swipe" fast enough across the device screen and only generate 2 or 3 points.One approach is to interpolate n-number of points on the lines between the
touchesMovedpoints, and then draw your shape image at each of those points.Here's some quick example code...
UIView subclass
Example controller class
Looks about like this: