NSMutableArray的线图CGPoints [英] Line Graph CGPoints from NSMutableArray

查看:50
本文介绍了NSMutableArray的线图CGPoints的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to adapt the code from the accelerometer example such that when the user depresses a uibutton a point is added to a line graph.

致力于将两个浮点数转换为CGPoint,然后将其转换为NSValue,然后使用以下内容将其添加到NSMutableArray:

Working on the converting two floats, which are the result of calculate as below into a CGPoint and converting the CGPoint into an NSValue and then adding this to a NSMutableArray with the following

-(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坐标.关于这个有什么想法吗?

But for some reason I am only getting one object stored in the array, it seems everytime push the calculate button the object pt gets overwritten, on the plus side it has the correct x,y coords. Any ideas on this one?

更新

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

Removed self.points = [NSMutableArray array]; and placed it in view did load, also set the first points to 0,0. so that is working ok. Now the next problem is that I have a Graph subclass where the CG Drawing is taking place. I am trying to figure out a simple way to be able to access the above NSMutableArray which is in a ViewController class from the graph class.

距离终点很近,但真的被卡住了,任何帮助都会很棒.仍在尝试在UIScrollview上的UIView上绘制折线图.draw rect方法位于UIView子类中,并且一切正常,在轴上具有网格线和标签,并且可以在其上手动绘制.但是我的问题是我无法读取CGPoints的NSMutableArray,它们是x和y坐标.ViewController执行计算,并将结果写入NSMutable数组,并且一切正常,我可以看到CGpoint及其值是通过ViewController中的NSLogs编写的.我尝试了各种方法来将NSMutableArray设置为全局方法,但无济于事,一切都会运行,但是虽然我可以看到在ViewController中写入的点,但它们对于UIView子类来说是不可见的.我也尝试过使用addObserver和observeValueForKeyPath方法,并且在所有子类都运行时再次无法看到该数组.任何想法,建议,技巧或想法都很棒

Am so close to the end but am really stuck, any help would be great. Still trying to draw a line graph on a UIView which is on a UIScrollview. The draw rect method is in the UIView Subclass and everything is working there, I have gridlines and labels on the axis and I can draw manually onto it. But the problem I have is that I cannot read the NSMutableArray of the CGPoints, which are the x and y coords. The ViewController performs a calculation and the results are written to the NSMutable array and this is all working fine as well, I can see the CGpoints and their values being written with NSLogs in the ViewController. I have tried various ways to set the NSMutableArray up as a global but to no avail, everything runs but while I can see the points being written in the ViewController they are just not visible to the UIView Subclass. I have also tried to use the addObserver and observeValueForKeyPath methods and once again while everything runs the subclass cannot see the array. Any ideas, suggestions, tips or thoughts would be great

推荐答案

让这一切正常工作,并且在整个过程中学到了很多东西,只是出于兴趣,最终产品的总体轮廓是

Got it all working and learnt a lot along the way, just out of interest the general outline of the final product is

  • 具有ScrollView和图形的UIView
  • 用于处理图形的UIView子类
  • NSMutable的单个类数组,以便ViewController可以编写值和UIView子类可以读取值以绘制图.

这是UIview子类中的相关内容

this is the relevant coed from the UIview subclass

- (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);

    }

}

这篇关于NSMutableArray的线图CGPoints的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆