【问题标题】:Line Graph CGPoints from NSMutableArray来自 NSMutableArray 的线图 CGPoints
【发布时间】:2011-01-11 05:44:01
【问题描述】:

我一直在尝试调整加速度计示例中的代码,以便在用户按下 uibutton 时将一个点添加到折线图中。

处理两个浮点数的转换,这是计算如下结果为 CGPoint 并将 CGPoint 转换为 NSValue,然后将其添加到 NSMutableArray 中

-(IBAction)calculate:(id)sender {   
    self.points = [NSMutableArray array];
    CGPoint pt = CGPointMake(d, c);
    [self.points addObject:[NSValue valueWithCGPoint:pt]];
    NSLog(@"%@", NSStringFromCGPoint(pt));
    NSLog(@"%@", [NSString stringWithFormat:@"%d points", self.points.count ]);
}   

但由于某种原因,我只将一个对象存储在数组中,似乎每次按下计算按钮时对象 pt 都会被覆盖,从好的方面来说,它具有正确的 x,y 坐标。关于这个有什么想法吗?

更新

移除 self.points = [NSMutableArray 数组];并将其放在视图中确实加载,还将第一个点设置为 0,0。这样就可以了。现在下一个问题是我有一个 Graph 子类,其中 CG 绘图正在发生。我试图找出一种简单的方法来访问上述 NSMutableArray,它位于图形类的 ViewController 类中。

我已经接近尾声了,但我真的被卡住了,任何帮助都会很棒。 仍在尝试在 UIScrollview 上的 UIView 上绘制折线图。 draw rect 方法在 UIView 子类中,一切都在那里工作,我在轴上有网格线和标签,我可以在上面手动绘制。但我遇到的问题是我无法读取 CGPoints 的 NSMutableArray,它们是 x 和 y 坐标。 ViewController 执行计算并将结果写入 NSMutable 数组,这一切正常,我可以在 ViewController 中看到 CGpoints 及其值是用 NSLogs 写入的。 我尝试了各种方法将 NSMutableArray 设置为全局但无济于事,一切都运行了,但是虽然我可以看到写在 ViewController 中的点,但它们对 UIView 子类不可见。 我还尝试使用 addObserver 和 observeValueForKeyPath 方法,当一切运行时,子类再一次看不到数组。 任何想法、建议、提示或想法都会很棒

【问题讨论】:

  • 删除 self.points = [NSMutableArray 数组];并将其放在视图中确实加载,还将第一个点设置为 0,0。一切正常

标签: iphone objective-c xcode nsmutablearray subclass


【解决方案1】:

一切顺利,一路上学到了很多东西,只是出于兴趣,最终产品的大致轮廓是

  • 带有 ScrollView 的 ViewController 和 图表的 UIView
  • UIView 子类处理绘图
  • NSMutable 的单例类 数组,以便 ViewController 可以写入 它和 UIView 子类的值 可以读取值来绘制 图表。

这是来自 UIview 子类的相关 coed

- (void)drawRect:(CGRect)rect {

    float what = self->m_Val1;
    NSLog(@"what %f",what);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGColorRef black = [[UIColor blackColor] CGColor];

    // Draw the grid lines
    DrawGridlines(ctx, 0.0, 200);


    // Draw the text
    UIFont *systemFont = [UIFont systemFontOfSize:12.0];
    [[UIColor whiteColor] set];
    [@"20" drawInRect:CGRectMake(2.0, 1.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
    [@"15" drawInRect:CGRectMake(2.0, 50.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
    [@"10" drawInRect:CGRectMake(2.0, 100.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
    [@"5" drawInRect:CGRectMake(2.0, 150.0, 24.0, 16.0) withFont:systemFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];


    if (what < 1) return;

    CGContextSetStrokeColorWithColor(ctx, black);
    CGContextSetLineWidth(ctx, 4.0f);
    MySingleton     *resultsInstance = [MySingleton instance];
    NSLog(@"From Graph view Count: %d", [[resultsInstance getArray] count]);
    float counthis =([[resultsInstance getArray] count]);
    for (int i = 0; i < (counthis-1); i++)
    {
        CGPoint pt1 = [[[resultsInstance getArray] objectAtIndex:i]CGPointValue]; 
        CGPoint pt2 = [[[resultsInstance getArray] objectAtIndex:i+1]CGPointValue]; 
        CGContextMoveToPoint(ctx, pt1.x, pt1.y);
        CGContextAddLineToPoint(ctx, pt2.x, pt2.y);
        CGContextStrokePath(ctx);
        NSLog(@"cgpoint %@", NSStringFromCGPoint(pt1));
        NSLog(@"point x,y: %f,%f", pt1.x, pt1.y);

    }

}

【讨论】:

    【解决方案2】:

    听起来你所有的 trackcalc[] 值都是零。反过来,这表明 [vallres.text floatValue] 返回 0。

    [NSString floatValue] 不能返回数字时返回零。

    floatValue

    返回接收方的浮点值 文本作为浮点数。

    - (float)floatValue

    返回值

    接收方的浮点值 文本作为浮点数,跳过空格 在字符串的开头。 返回 HUGE_VAL 或 –HUGE_VAL on 上溢,下溢为 0.0。还 如果接收者没有,则返回 0.0 以有效的文本表示开始 浮点数。

    【讨论】:

    • 感谢 westsider,我在 NSMutableArray 方法方面取得了相当大的进步。
    猜你喜欢
    • 1970-01-01
    • 2011-01-29
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多