【发布时间】:2014-01-15 00:16:11
【问题描述】:
我目前有一个计时器,它在下面动画一条圆形线 - 一条倒计时线。这会根据我传递给 _percent 的秒数来制作动画。
我想要另一个描绘秒的圆形轮子。但是,我正在努力让动画在 _percent 中每一秒持续 1 秒。例如如果百分比是 100,我希望循环动画 100 次,持续 1 秒。 (以及实现下图的动画效果。
为了清楚起见,这里是一张图:) 第一行 3 显示了它现在的样子,最后一行 3 显示了我想要的效果......
这是我在视图控制器类中触发计时器的方法:
-(void)viewDidLoad
{
CPCircleCountdownView *m_testView;
m_testView.percent = 100;
}
- (IBAction)StartTimerButtonPressed:(id)sender
{
// Kick off a timer to count it down
m_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(decrementSpin) userInfo:nil repeats:YES];
}
- (void)decrementSpin
{
// If we can decrement our percentage, do so, and redraw the view
if (m_testView.percent > 0) {
m_testView.percent = m_testView.percent - 1;
[m_testView setNeedsDisplay];
}
else {
[m_timer invalidate];
m_timer = nil;
}
}
这是我的 CPCountdownView,我想在其中添加新的圆形动画,每剩下一秒钟持续一秒钟。理论上两个动画应该同时完成。
#import "CPCircleCountdownView.h"
@interface CPCircleCountdownView ()
{
CGFloat startAngle;
CGFloat endAngle;
}
@end
@implementation CPCircleCountdownView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
//get start and end angles
startAngle = M_PI * 1.5;
endAngle = startAngle + (M_PI * 2);
}
return self;
}
- (void)drawRect:(CGRect)rect
{
//Display arc percent
//NSString *textContent = [NSString stringWithFormat:@"%d", self.percent]; //TODO: Pass total number of seconds int
//-------------------------
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
// Create our arc, with the correct angles
[bezierPath addArcWithCenter:CGPointMake(rect.size.width / 2, rect.size.height / 2)
radius:130
startAngle:startAngle
endAngle:(endAngle - startAngle) * (_percent / _overallScore) + startAngle
clockwise:YES];
// Set the display for the path, and stroke it
bezierPath.lineWidth = 2;
[[UIColor colorWithRed:122.0 / 255.0 green:197.0 / 255.0 blue:205.0 / 255.0 alpha:1.0] setStroke];
[bezierPath stroke];
//-------------------------
}
任何帮助都会很棒我相对较新,所以如果你能给我一些很好的例子:)谢谢
【问题讨论】:
标签: ios objective-c nstimer countdown uibezierpath