1 year ago

#291328

test-img

Aamir

Update Frame of Rotated UIView

I am trying to update the size of rotated UIView. I tried using CGAffineTransform for rotation but whenever I tried updating the size after rotation it messed up the frame.

I somehow managed to update size of rotated view using this stackoverflow answer: rotate-uiimage-and-move which suggests:

  1. Preserving original transform and assigning .identity to the view's transform
  2. Updating frame of view
  3. Reassign original transform

Here is my final code which is working perfectly:

    func handlePinch(_ gesture: UIPinchGestureRecognizer) {
        // Preserve previous transform and assign .identity
        let transform = myView.transform
        myView.transform = .identity

        // Compute new frame
        let updatedOrigin = myView.frame.origin.applying(transform.scaledBy(x: gesture.scale, y: gesture.scale))
        let updatedSize = CGSize(width: myView.frame.width * gesture.scale, height: myView.frame.height * gesture.scale)
        myView.bounds = CGRect(origin: updatedOrigin, size: updatedSize)

        // Assign back original transform
        textView.transform = transform

        gesture.scale = 1

    }

Questions:

  1. Is this the right approach to do that? To me it seems like a hack as transform using frame's origin to compute new origin which only works fine when assigned to bounds origin.
  2. What is the best way to achieve resizing a rotated view?

ios

swift

cgaffinetransform

0 Answers

Your Answer

Accepted video resources