如何使用CoreAnimation像时钟倒计时一样为圆形设置动画 [英] How to use CoreAnimation to animate a circle like a clock countdown

查看:146
本文介绍了如何使用CoreAnimation像时钟倒计时一样为圆形设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个时间倒计时指示器就像一个圆圈,从一个拱形开始到一个完整的圆圈。

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!

推荐答案

当您更改其中一个角度时,SetNeedsDisplay可以进行一些优化以仅重绘受影响的区域。把这样的东西放到你的视图的绘图功能中。祝你好运

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);

这篇关于如何使用CoreAnimation像时钟倒计时一样为圆形设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆