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.
The problem is, when the scaling animation occurs, the corner radius of my UIView is "squished":
How can I ensure the corner radius remains perfectly rounded, while maintaining the scaling animation?
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.
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With