I have a UIView
called viewProgress
. It is the white box in the image. I want a circular progress bar, which is the green circle in the image. The progress bar should stay within the white box, but as you can see it is way off.
How do I make it stay inside the viewProgress
?
Here you have my animation function:
func animateView() {
let circle = viewProgress // viewProgress is a UIView
var progressCircle = CAShapeLayer()
let circlePath = UIBezierPath(arcCenter: circle.center, radius: circle.bounds.midX, startAngle: -CGFloat(M_PI_2), endAngle: CGFloat(3.0 * M_PI_2), clockwise: true)
progressCircle = CAShapeLayer ()
progressCircle.path = circlePath.CGPath
progressCircle.strokeColor = UIColor.whiteColor().CGColor
progressCircle.fillColor = UIColor.clearColor().CGColor
progressCircle.lineWidth = 10.0
circle.layer.addSublayer(progressCircle)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 0.4
animation.duration = 1
animation.fillMode = kCAFillModeForwards
animation.removedOnCompletion = false
progressCircle.addAnimation(animation, forKey: "ani")
}
And I'm just calling the function inside viewDidLoad()
Basically u have few problems:
animation.repeatCount = MAXFLOAT
layoutSubviews
recreate layers, with removing previously created oneUIBezierPath(arcCenter...
will start path fromrightSide
, u can also use simpler variant likeUIBezierPath(ovalInRect
. With this in mind u also need to rotate layer for M_PI_2 if u want to make start of drawing in the very top of circleclipSubviews
of parentView. To prevent this use modified parentLayer rectlineCap
, buy setting it to "round" styleprogressCircle.addAnimation(animation, forKey: "ani")
key for anim not required if u dont want to get completion event from animation - i your case i see there is nodelegate
and noremoveOnCompletion
flag setted to false, so make assumption that u don`t actually need itKeeping this all in mind code can be like :
and result actually like:
Note: If u want to animate rotation of animated layer u should also add additional animation in to anim group