开发者

Line Graph CGPoints from NSMutableArray

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.

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 ]);
}   

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?

UPDATE

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.开发者_C百科 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.

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

  • ViewController with ScrollView and UIView for the graph
  • UIView subclass to handle the drawing
  • Singleton class for the NSMutable array so the ViewController can write values to it and the UIView subclass can read the values to draw the graph.

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

    }

}


It sounds like all of your trackcalc[] values are zero. Which, in turn, suggests that [vallres.text floatValue] is returning 0.

[NSString floatValue] returns zero when it can't return a number.

floatValue

Returns the floating-point value of the receiver’s text as a float.

- (float)floatValue

Return Value

The floating-point value of the receiver’s text as a float, skipping whitespace at the beginning of the string. Returns HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Also returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜