SVGImageView Pinch Gesture changing frame issue.
ajaydexati opened this issue · 1 comments
ajaydexati commented
class SimpleDetailViewController: UIViewController {
var svgImageView : SVGImageView!
init(svgURL: URL) {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .white
svgImageView = SVGImageView.init(contentsOf: svgURL)
svgImageView.contentMode = .scaleAspectFit
view.addSubview(svgImageView)
svgImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
svgImageView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 12),
svgImageView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -12),
svgImageView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 12),
svgImageView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor, constant: -12),
])
let pinchGesture = UIPinchGestureRecognizer.init(target: self, action: #selector(pinchTranslate(_:)))
svgImageView.addGestureRecognizer(pinchGesture);
}
override func viewDidLoad() {
super.viewDidLoad()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static var pinchTranslateBoundsBeforeScaling = CGRect.zero
static var pinchTranslateTransformBeforeScaling: CGAffineTransform?
@objc func pinchTranslate(_ recognizer: UIPinchGestureRecognizer?) {
if recognizer?.state == .began {
SimpleDetailViewController.pinchTranslateBoundsBeforeScaling = recognizer?.view?.bounds ?? CGRect.zero
SimpleDetailViewController.pinchTranslateTransformBeforeScaling = recognizer?.view?.transform
}
let center = recognizer?.view?.center
let scale: CGAffineTransform = CGAffineTransform.identity.scaledBy(x: recognizer!.scale, y: recognizer!.scale)
var frame = SimpleDetailViewController.pinchTranslateBoundsBeforeScaling.applying(scale)
frame.origin = CGPoint(x: (center?.x ?? 0.0) - frame.size.width / 2, y: (center?.y ?? 0.0) - frame.size.height / 2)
recognizer?.view?.transform = .identity
recognizer?.view?.frame = frame
if let pinchTranslateTransformBeforeScaling = SimpleDetailViewController.pinchTranslateTransformBeforeScaling {
recognizer?.view?.transform = pinchTranslateTransformBeforeScaling
}
}
}
Applying pinch gesture to zoom SVGImageView Frame result in delay and unwanted animation.
arielelkin commented
Hi @ajaydexati
I can see that you do the zoom using a CGAffineTransform
, but I need more information about how SimpleDetailViewController
is setup in order to assess the issue.
Instead of using CGAffineTransforms
, I can recommend that you implement zooming by placing the imageview within a scrollview, returning it as the scrollview delegate's viewForZoomingInScrollView
, and letting the scrollview do the zooming. If that doesn't work, feel free to reopen this issue with more details.