How to use CoreAnimation to animate a circle like a clock countdown

天涯浪子 提交于 2019-12-03 23:19:48

问题


I wish to have a time countdown indicator just like a circle to start from a slice of arch to a full circle.

I have tried several ways, both ended up with the arch turning directly to be a full circle, not go clockwise. Shown in demo pics as following:

Any idea? Greatly appreciated!


回答1:


SetNeedsDisplay when you change one of the angles, you can do some optimizations to only redraw the affected area. Put something like this into your draw function of your view. Good Luck

CGContextRef myContext = UIGraphicsGetCurrentContext();
CGPoint center = { self.bounds.size.width * 0.5f, self.bounds.size.height * 0.5f };


CGContextClearRect(myContext, rect);

CGContextMoveToPoint(myContext, center.x, center.y);
// start and end are in radians.
CGContextAddArc(myContext, center.x, center.y, MIN(self.frame.size.width,self.frame.size.height)*0.5f, _startAngle, _endAngle, NO);
CGContextAddLineToPoint(myContext, center.x, center.y);
CGContextClip(myContext);

CGContextSetFillColorWithColor(myContext,[UIColor redColor].CGColor);
CGContextFillRect(myContext, self.bounds);


来源:https://stackoverflow.com/questions/8895244/how-to-use-coreanimation-to-animate-a-circle-like-a-clock-countdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!