Search code examples
iosswiftuiviewanimation

Resizable UIView


I am building a custom UIView that you can rotate and resize. I can resize the UIView by dragging the corners of the UIView. I calculate how much I have dragged then change the frame of the UIView accordingly.

However, I am running into problems once I added a rotation gesture recognizer to the view. If I rotate or apply a transform to the view, I no longer know how to calculate drag distance and change the frame of the view. How could I calculate the width and height change between my new view and the original view when things are put at an added angle or if they have some other transform, like a translation transform?

I thought of possibilities to set the view's transform back to .identity, change the size of the view, then re-apply its transform, but I'm not sure how to actually go about implementing this.


Solution

  • After applying transform you can not use frame

    You have two options

    1) First Calculate everything using center of your view

    2) As you know apply identity and change frame

    for point 2 I have added example that might helpful to you

        let transform = imageView.transform
        imageView.transform = CGAffineTransform.identity
        var rect: CGRect = imageView.frame
    
        rect = // Change Rect here       
    
        imageView.frame = rect // Assign it 
        imageView.transform = transform // Apply Transform