我正在尝试使用 NSTimer 为 Core Plot 图形制作动画
- (void) animate
{
CGPoint *moved = [array objectAtIndex:0];
for (float i = 1.43; i<=2.91; i+=0.01)
{
moved.x = i;
moved.y = i;
if (timer != nil)
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(reloadPlotData) userInfo:nil repeats:NO];
}
}
-(void) reloadPlotData
{
[graph reloadData];
}
但是没有动画。仅在循环结束时重新加载数据
我也试过 [NSThread sleepForTimeInterval:1],但还是有同样的问题
我该如何修复它?
抱歉英语不好.. :)
最佳答案
我不明白你是如何使用 moved
(CGPoint
是一个结构,而不是一个对象)。不过,这不是您的问题。
每次通过该循环,您都在重置计时器,远在它有机会触发之前。如果您希望图形每秒更新一次,请摆脱 -animate
中的循环,创建一个重复计时器,在 -reloadPlotData
中增加计数器,并在你得到最后一个值(例如,你的例子中的 2.91)。显然,您需要将计数器存储在实例变量或将在 -reloadPlotData
调用之间持续存在的其他范围。
例如(未经测试):
@接口(interface)
myPoint *moved; // custom class with x and y properties
NSTimer *timer;
@实现
- (void) animate
{
moved = [array objectAtIndex:0];
float start = 1.43;
moved.x = start;
moved.y = start;
if (timer != nil)
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(reloadPlotData)
userInfo:nil
repeats:YES];
}
-(void) reloadPlotData
{
moved = [array objectAtIndex:0];
moved.x += 0.01f;
moved.y += 0.01f;
if ( moved.x > 2.91 ) {
[timer invalidate];
timer = nil;
}
else {
[graph reloadData];
}
}
关于objective-c - NSTimer 的 Core Plot 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6060360/