我有一堆用UIBezierPath构建的CAShapeLayers并存储在UIScrollView中。
我想知道当UIScrollView缩放超过100%时,是否有任何方法可以保持CAShapeLayer路径的lineWidth远离缩放。我的意思是将UIScrollView的转换规则应用于CAShapeLayer,但保持路径与以前在1.0 zoomScale上的路径相同。 (因此当具有1pt路径lineWidth的图层将被缩放到300%时,@ 2x设备上的图层的lineWidth将变为6px高度,但将保持2px的高度)
答案 0 :(得分:0)
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)
}
}
这是你想要的吗?