exyte / Macaw

Powerful and easy-to-use vector graphics Swift library with SVG support
MIT License
6.01k stars 553 forks source link

Unable to scale smoothly #721

Open nikogamulin opened 4 years ago

nikogamulin commented 4 years ago

I checked all the issues related to scaling/zooming and haven't found a way to enable smooth scaling. Finally, I have implemented zooming the following way:

extension PaintScrollView : UIScrollViewDelegate {
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return paintView
    }

    public func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        DispatchQueue.main.async {
            self.paintView.contentScaleFactor = scale + 5
            self.paintView.layoutIfNeeded()
            print("I just finished zooming")
        }
    }
}
extension PaintScrollView: UIGestureRecognizerDelegate {
    public override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let recognizer = gestureRecognizer as? UIPinchGestureRecognizer {
            let location = recognizer.location(in: self)
            let scale = self.scaleCurrent * Double(recognizer.scale)
            let anchor = Point(x: Double(location.x), y: Double(location.y))
            let node = self.paintView.node
            let dx = anchor.x * (1.0 - scale)
            let dy = anchor.y * (1.0 - scale)
            node.place = Transform.move(dx: dx, dy: dy).scale(sx: scale, sy: scale)

        }
        return true
    }
    public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

In case of this implementation, however, scrollViewDidEndZooming is never called and the performance is far from smooth. I noticed, there have been many questions asked related to zooming already but I haven't found any clear solution.

ystrot commented 4 years ago

Hi @nikogamulin

Did you try to use MacawView.zoom feature? It's available in the master branch.