samedi 24 octobre 2009

NSMutableArray and CGPointArray

We wanted to set up an array of CGPoints but failed, in the properties....
We had to do it with a NSMutableArray. But how to get the CGPoints in?

the answer is found here:

http://stackoverflow.com/questions/943945/how-to-form-cgpoint-array-in-objective-c

so:

@interface OrbitLinesDrawView : UIView {

UIColor *strokeColor;
UIColor *rectColor;
CGFloat strokeWidth;
CGFloat cornerRadius;

NSMutableArray* orbitLinePointsArray ;
}

@property (nonatomic, retain) NSMutableArray* orbitLinePointsArray ;

and then in the .m file:

@synthesize orbitLinePointsArray;

in the init:

orbitLinePointsArray = [[NSMutableArray alloc]init ];

for (int i = 0; ... etc....)
CGPoint aPoint = CGPointMake(100 - 10*i, 200+10*i);
NSValue* pointValueObject = [NSValue valueWithCGPoint:aPoint];
[orbitLinePointsArray addObject:(NSValue*)pointValueObject];
}

(dont forget to alloc the orbitLinePointsArray)

for setting the CGPoints later on:

-(void)setObjectInPointArray: (int)positionInArray : (CGPoint) myPoint
{
NSValue* pointValueObject = [NSValue valueWithCGPoint:myPoint];
[ orbitLinePointsArray replaceObjectAtIndex:positionInArray withObject:pointValueObject ];
}

drawing the points in the

-(void)drawLines {

...
...
...

CGPoint lineArray[2];

for(int i=0 ; ... etc....)
{
lineArray[0] = ((CGPoint) [[orbitLinePointsArray objectAtIndex:i] CGPointValue]);
lineArray[1] = ((CGPoint) [[orbitLinePointsArray objectAtIndex:i+1] CGPointValue]);

CGContextAddLines ( context, lineArray, 2 );
CGContextStrokePath(context);
}

[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {

[ self drawLines ];

}

so Objective C, Cocoa, ....whatever....wrap it up in further and further objects, that seems to be Apple.....