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.
swift core-animation cornerradius
Sethmr Jan 07 '17 at 16:25 2017-01-07 16:25
source share