How to increase UIView in UIView.animate () with exact corner radius?

I have this SwiftySwitch project.

My goal is to make the circle expand and reduce the retention of the shape of the circle, rather than having a radius of an angle that is greater than the current height of the circle at any given moment in the animation.

This circle processing code is here:

func turnOn() { let tempView = UIView(frame: CGRect(x: ballDiameter / 2, y: ballDiameter / 2, width: 0, height: 0)) tempView.backgroundColor = onColor tempView.layer.cornerRadius = ballDiameter / 2 self.addSubview(tempView) UIView.animate(withDuration: dotTravelTime, animations: { [weak self] in //RIGHT HERE is the code for the circle to hold it circular form if self != nil { tempView.frame = CGRect(x: 0, y: 0, width: self!.ballDiameter, height: self!.ballDiameter) tempView.layer.cornerRadius = self!.ballDiameter / 2 } self?.layoutIfNeeded() }) { [weak self] _ in self?.backgroundColor = self!.onColor tempView.removeFromSuperview() } } func turnOff() { let tempView = UIView(frame: CGRect(x: 0, y: 0, width: ballDiameter, height: ballDiameter)) tempView.backgroundColor = onColor self.addSubview(tempView) self.backgroundColor = offColor UIView.animate(withDuration: dotTravelTime, animations: { [weak self] in //RIGHT HERE is the code for the circle to hold it circular form if self != nil { tempView.frame = CGRect(x: self!.ballDiameter / 2, y: self!.ballDiameter / 2, width: 0, height: 0) tempView.layer.cornerRadius = self!.ballDiameter / 2 } self?.layoutIfNeeded() }) { _ in tempView.removeFromSuperview() } } 

Where I say RIGHT HERE is the place where I process the circle animation. I use a temporary UIView to handle the animation, and then delete the view after changing the color of the actual view behind it. TempView is a circle toggle that you see with sliding animation. If you need information about my design, let me know. I tried a lot of things and everything was allowed when the circle was a square or had a slightly larger angular radius than I wanted. (I think that it reduces the radius of the angle from the original, but it is insignificant.

0
swift core-animation cornerradius
Jan 07 '17 at 16:25
source share
2 answers

My goal is for the circle to expand and reduce the retention of the shape of the circle

Then do not make the circle the β€œstupid” way ( cornerRadius ). Mask on a circular path and animate the growth / shrinkage of the mask (and / or path).

In this example, I express a yellow look along with its mask. The animation is intentionally slow (2 second duration) to show you that it runs smoothly. The yellow view contains a label in the upper left corner and a label in the lower right, so you can see that (1) it is a view, and (2) it grows coherently.

enter image description here

+2
Jan 7 '17 at 16:46 on
source share

The corner radius is a CALayer property, not a UIView. Unfortunately, UIView animation methods will only collect key value changes for properties associated with UIView. You can animate the properties of the layer separately, but you need to do this with CABasicAnimation. Alternatively, you can implement drawRect and just draw a circle with UIBezierPath instead of using the corner radius.

+1
Jan 07 '17 at 16:52
source share



All Articles