【发布时间】:2011-05-03 10:23:48
【问题描述】:
嗨,我目前正在使用 core-plot 绘制动态图并且工作正常,现在我正在考虑在 2 个单独的视图上添加 2 个图
我已经开始使用 Mainview 绘图 Graph-1 和 Flipview 绘图 Graph-2 的“实用程序”
主视图图形工作正常,图形每 2 秒更新和重新加载一次,但是当我触摸“flipviewWindow”时,graph2 总是以新值和新的 graph2data 数组开始(可能是因为 graph2timer 再次被调用??? ) 但 graph1data 仅使用一个 NSTimer 维护并且工作正常。
我不想发布这些值,因为我想查看具有先前值的图形,例如 Graph1。
有人可以建议如何通过动态数据更新在 2 个单独的 UIview 上实现 2 个核心图吗? 我的图表逻辑如下
In MainViewController.m
-(void)viewDidLoad{
[super viewDidLoad];
[self ConstructGraph1]
}
-(void)ConstructGraph1 {
graph1 = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:kCPStocksTheme];
[graph1 applyTheme:theme];
mainViewWindow.hostedGraph=graph1;
.
.
.
.
.
myTimer1=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(graph1Timer:) userInfo:nil repeats:YES];
}
-(void) graph1Timer: (NSTimer *) Timer{
NSTimeInterval aTimeInterval1 = [[NSDate date]
timeIntervalSinceReferenceDate];
Get JSON request and add object at every 2 sec, after succesfully fetching
[graph1data addObject:...]
[graph1 reloadData];
}
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showFlipWindow:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
In FlipViewController.m
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[self ConstructGraph2]
}
//ConstructGraph2 uses the same logic to draw the graph and to fetch the results
-(void) ConstructGraph2 {
graph2 = [[CPXYGraph alloc] initWithFrame:CGRectZero];
CPTheme *theme = [CPTheme themeNamed:kCPStocksTheme];
[graph1 applyTheme:theme];
mainViewWindow.hostedGraph=graph1;
.
.
.
.
.
myTimer2=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(graph2Timer:) userInfo:nil repeats:YES];
}
-(void) graph2Timer: (NSTimer *) Timer{
NSTimeInterval aTimeInterval2 = [[NSDate date]
timeIntervalSinceReferenceDate];
Get JSON request and add object at every 2 sec, after succesfully fetching
[graph2data addObject...];
[graph2 reloadData];
}
- (IBAction)showMainWindow:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
【问题讨论】:
-
myTimer1和myTimer2的作用域是什么?
-
@Paul Ardeleanu myTimer1 和 myTimer2 获取添加到各自数组(用于绘图)的 JSON 值,重新计算 core-plot Xrange 并重新加载图表。 (所以图表随着数据点的进入而移动)。事实上我只需要运行一次调用这个计时器并让它们运行。但是当我做翻转时,只有 timer2 被再次(随机)调用,graph1 和timer1 很好,它们只有一个实例在运行……所以不知道该怎么做……
-
Linus,我认为您应该将检索过程与显示分开。此外,您应该在单独的线程(异步)上运行数据获取。
-
另外,您可以使用 NSNotifications 让主线程知道有新数据进入,并且需要重新绘制图形。不要使用计时器刷新视图 - 这应该仅在响应“刷新”请求时刷新。
标签: nstimer core-plot flip nstimeinterval