objective-c - NSTimer 的 Core Plot 动画

标签 objective-c ios nstimer core-plot

我正在尝试使用 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/

相关文章:

屏幕解锁或应用切换时的 iOS View / Controller 生命周期回调

iphone - 在 Xcode 中以编程方式按下 UITabBar 按钮

ios - UITableViewCell 分隔符拒绝消失且 NSTextAlignmentRight 不生效

objective-c - Objective-C 中初始化对象的不同方式

ios - 检查 UITextfield 只有字母没有数值

ios - 是否可以在 iTunes Connect 上识别退款?

ios - 谷歌地图 ios sdk 中心的固定标记

objective-c - SoundCloud 错误(clang 失败,退出代码为 1)

ios - 如何使用 Swift 选择器?选择器不断将结果变为 `Type has no member`

ios - 带有通知问题的 Swift 暂停和重新创建计时器