Search code examples
swiftuiviewcgaffinetransform

Unable to increase height and width of UIView when using CGAffineTransformMakeRotation


I am rotating UIView by using UISLider Values. If slider values either get increase or decrease, according to that rotation happen. My code is working fine.

Rotation Action:

@IBAction func sliderToolAction(sender: UISlider) {

     if sender.tag == 1
     {
       let radians = CGFloat(sender.value) * 22 / (7 * 180)
       rotatingView.transform = CGAffineTransformMakeRotation(radians)
     }
}

Output:

Rotating Frame:  (100, 20, 103.9990, 127.8889) 

After rotating that UIView, I tried to increase width and height of that view, its not working.

Resize Action

@IBAction func sliderToolAction(sender: UISlider) {

     if sender.tag == 2
     {
       rotatingView.frame.size.width = CGFloat(sender.value) * viewWidth
       rotatingView.frame.size.height = CGFloat(sender.value) * viewHeight
     }
}

Output:

Rotating Frame:  (80, 40, 207.8888, 255.6667)  //ATTEMPT 1
Rotating Frame:  (50, 60, 207.8888, 255.6667)  //ATTEMPT 2
Rotating Frame:  (20, 90, 207.8888, 255.6667)  //ATTEMPT 3
Rotating Frame:  (-10, 120, 207.8888, 255.6667)  //ATTEMPT 4

I am increasing width and height alone, but UIView position is moving out of screen. Kindly guide me on this.


Solution

  • Straight from Apple's documentation:

    Warning

    If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

    and:

    Changes to this property can be animated. However, if the transform property contains a non-identity transform, the value of the frame property is undefined and should not be modified. In that case, you can reposition the view using the center property and adjust the size using the bounds property instead.

    Instead of changing the frame size, change the bounds size.