0

I have an UIView placed over an UIImageView and I am trying to pinch-zoom the UIView over the image view without applying transform. The code is as below where something goes wrong which makes the UIview zoom-in/out drastically and moves out of the image view when even a little pinch is applied. What am I doing wrong ?

@objc func didPinch(sender: UIPinchGestureRecognizer) {     
    guard let subView = sender.view else { return }
    var subViewFrame = subView.frame
    let subViewCenter = subView.center
    if sender.state == .changed {
        let scale = sender.scale
        subView.frame = CGRect(x: 0, y: 0, width: subViewFrame.width * scale, height: subViewFrame.height * scale)
        subView.center = subViewCenter
    }
}

Note: I understand that applying transform is a straight forward and an easy way to scale the view but I am not opting that as my UIView has border width applied to it and when the UIView is scaled, the scaling applies to the border width too, which I am looking to avoid.

XiOS
  • 1,665
  • 2
  • 18
  • 22
  • check if u are adding the gesture recogniser to the correct view. ```subViewFrame.width``` might return the width of a super view or something else. – udi Aug 16 '22 at 08:00
  • why don't you create custom view which contains two view one is border view other is your content view to scale and manage frame as content view frame width and height - border width and height. and border view frame should main frame and add border width to border view then. self.borderView.autoresizingMask = [.flexibleWidth, .flexibleHeight] ... then apply transform to view ? – Zeeshan Ahmad II Aug 16 '22 at 08:06
  • 1
    Don't you have to set the `scale` of the gesture recogniser back to `1`? [Like in this answer?](https://stackoverflow.com/a/58558272/5133585) – Sweeper Aug 16 '22 at 08:12
  • @Sweeper, thanks, that worked for my case.. I was in a perception that I need not reset the scale as I wasn't using transform function directly. that helped. thanks. – XiOS Aug 26 '22 at 07:00

0 Answers0