How can we have a custom mouse pointer one a view using SwiftUI + iPadOS?
Apple has an example of how to do that in this code but is for UIKit: https://developer.apple.com/documentation/uikit/pointer_interactions/enhancing_your_ipad_app_with_pointer_interactions
class QuiltView: UIView, UIPointerInteractionDelegate {
...
func sharedInit() {
...
// Add a pointer interaction for the "editor" area Quilt view.
self.addInteraction(UIPointerInteraction(delegate: self))
}
// Supply custom regions that correspond to grid lines when "useStraightLineStitch" is enabled.
func pointerInteraction(_ interaction: UIPointerInteraction,
regionFor request: UIPointerRegionRequest,
defaultRegion: UIPointerRegion) -> UIPointerRegion? {
if useStraightLineStitch {
let regionNumber = floor(request.location.y / gridHeight)
return UIPointerRegion(rect: CGRect(x: 0, y: regionNumber * gridHeight, width: self.bounds.size.width, height: gridHeight))
} else {
return defaultRegion
}
}
// Supply pointer style with custom crosshair shape.
func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
let crosshair = UIPointerShape.path(crosshairBezierPath())
if useStraightLineStitch {
return UIPointerStyle(shape: crosshair, constrainedAxes: [.vertical])
} else {
return UIPointerStyle(shape: crosshair)
}
}
func crosshairBezierPath() -> UIBezierPath {
let crosshairPath = UIBezierPath()
crosshairPath.move(to: CGPoint(x: -1.5, y: 1.5))
crosshairPath.addLine(to: CGPoint(x: -1.5, y: 10))
crosshairPath.addArc(withCenter: CGPoint(x: 0, y: 10),
radius: 1.5,
startAngle: CGFloat.pi,
endAngle: 0,
clockwise: false)
crosshairPath.addLine(to: CGPoint(x: 1.5, y: 1.5))
crosshairPath.addLine(to: CGPoint(x: 10, y: 1.5))
crosshairPath.addArc(withCenter: CGPoint(x: 10, y: 0),
radius: 1.5,
startAngle: CGFloat.pi / 2.0,
endAngle: CGFloat.pi * 1.5,
clockwise: false)
crosshairPath.addLine(to: CGPoint(x: 1.5, y: -1.5))
crosshairPath.addLine(to: CGPoint(x: 1.5, y: -10))
crosshairPath.addArc(withCenter: CGPoint(x: 0, y: -10),
radius: 1.5,
startAngle: 0,
endAngle: CGFloat.pi,
clockwise: false)
crosshairPath.addLine(to: CGPoint(x: -1.5, y: -1.5))
crosshairPath.addLine(to: CGPoint(x: -10, y: -1.5))
crosshairPath.addArc(withCenter: CGPoint(x: -10, y: 0),
radius: 1.5,
startAngle: CGFloat.pi * 1.5,
endAngle: CGFloat.pi / 2.0,
clockwise: false)
crosshairPath.close()
return crosshairPath
}
...
}
What I'm looking for is to achieve the same but using SwiftUI? thanks in advance