Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGAffineTransform affects the corner radius of my UIView

I'm applying a CGAffineTransform animation to a UIView with the following code:

UIView.animate(withDuration: 0.5, delay: delay, options: [.autoreverse, .repeat], animations: {
    elementView.transform = CGAffineTransform(scaleX: 1.0, y: 0.4)
}) { (finished) in

}

The UIView scales correctly in the animation, but I also have the UIView's corner radius set to perfectly round the top and bottom of the view.

enter image description here

The problem is, when the scaling animation occurs, the corner radius of my UIView is "squished":

enter image description here

How can I ensure the corner radius remains perfectly rounded, while maintaining the scaling animation?

like image 216
hgwhittle Avatar asked Feb 06 '18 21:02

hgwhittle


People also ask

How do you round UIView corners?

If you start with a regular UIView it has square corners. You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.

How do you set corner radius for UIView in storyboard?

Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)

What is corner radius in IOS?

Discussion. Setting the radius to a value greater than 0. 0 causes the layer to begin drawing rounded corners on its background. By default, the corner radius does not apply to the image in the layer's contents property; it applies only to the background color and border of the layer.


1 Answers

I think your best bet is to just change the frame property of elementView:

UIView.animate(withDuration: 0.5, delay: delay, options: [.autoreverse, .repeat], animations: {
    elementView.frame = CGRect(x: elementView.frame.origin.x, y: elementView.frame.origin.y, width: elementView.frame.size.width, height: elementView.frame.size.height * 0.4)
}) { (finished) in

}

or if you're using auto-layout:

heightContraint.constant = heightConstraint * 0.4
UIView.animate(withDuration: 0.5, delay: delay, options: [.autoreverse, .repeat], animations: {
    elementView.layoutIfNeeded()
}) { (finished) in

}

Either way should keep your corner radius while shortening the view.

like image 51
Jake Avatar answered Sep 19 '22 11:09

Jake