class ViewController : UIViewController, UIScrollViewDelegate {
private lazy var scrollView = UIScrollView()
private lazy var contentView = UIView()
private lazy var shapeLayer = CAShapeLayer()
private let lineWidth: CGFloat = 1
override func loadView() {
view = UIView()
view.addSubview(scrollView)
scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
scrollView.addSubview(contentView)
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 2.0
scrollView.delegate = self
contentView.backgroundColor = .red
let viewSize = CGSize(width: 400, height: 400)
scrollView.contentSize = viewSize
contentView.frame = CGRect(x: 0, y: 0, width: viewSize.width, height: viewSize.height)
setUpShapeLayer(in: viewSize)
}
//MARK: - UIScrollViewDelegate
func scrollViewDidZoom(_ scrollView: UIScrollView) {
shapeLayer.lineWidth = lineWidth * 1 / scrollView.zoomScale
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return contentView
}
//MARK: - Private
private func setUpShapeLayer(in size: CGSize) {
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.strokeColor = UIColor.black.cgColor
shapeLayer.lineWidth = lineWidth
contentView.layer.addSublayer(shapeLayer)
let shapeSize = CGSize(width: 200, height: 200)
let shapeBounds = CGRect(origin: .zero, size: shapeSize)
shapeLayer.bounds.size = shapeSize
shapeLayer.path = UIBezierPath(ovalIn: shapeBounds).cgPath
shapeLayer.position = CGPoint(x: size.width / 2, y: size.height / 2)
}
}
Is it what you want ?