I have been trying to scroll and zoom a CALayer, specifically a CAShapeLayer, of several polygon paths. I added the CALayer to a UIScrollView which has successfully enabled scrolling around the CALayer.
However, pinch to zoom is not working. Several tutorials have implemented zooming a UIImageView with the UIScrollViewDelegate essentially like so:
@IBOutlet var scrollView : UIScrollView!
var imageView = UIImageView()
scrollView.addSubview(imageView)
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
But CALayer is incompatible with UIView and I have found no information on reconciling the difference.
Is there a similar native way to zoom a CALayer in Swift? Is it something really simple that escaped me? Any help would be appreciated; apologies if I am missing the obvious.
After much digging I found the related documentation from Apple. Turns out pinch-to-zoom is handled automatically on CALayers if they are set up correctly. I adapted the demo code for Swift and came up with this basic structure, which worked for me.
I hope this helps someone else!