3

I would like to store the points whenever touchesMoved is called. So far everything is working fine. However what I want to do is to create a "new" array every time touchesMoved is called. For example, once the touch ended the array of points are then saved into database with an identifier of 1. The next time the touchesMoved is called, the array is emptied and replaced with another set of points with a different identifier. I tried incrementing an integer in touchesEnded every time it is called, but I figured out that the integer will remain the same every time the touch ended. So how will I do this? Any help will be very much appreciated.

UPDATE: For example I have this recorded points in my array:

100.000, 200.000
100.000, 202.000
100.000, 204.000

This points will be saved in the database with a identifier, let's say 1.

1 | 100.000 | 200.000
1 | 100.000 | 202.000
1 | 100.000 | 204.000

Now, when the touches moved and ended again, the new set of recorded points will be saved with a different identifier, let's say 2.

2 | 200.000 | 300.000
2 | 200.000 | 302.000
2 | 200.000 | 304.000

So basically what I want to happen is that every time the touchesMoved is called, it will record points that will be saved in an array. When the touches ended, this recorded points will be saved with an identifier. AND, once the touchesMoved is called again and ended, the new set of recorded points will be saved with a different identifier. Any ideas how to do it?

Anna Fortuna
  • 1,071
  • 2
  • 17
  • 33

1 Answers1

12

To store CGPoint in NSMutableArray, do this

NSMutableArray *yourCGPointsArray = [[NSMutableArray alloc] init];
[yourCGPointsArray addObject:[NSValue valueWithCGPoint:CGPointMake(100, 100)]];

//Now getting the cgpoint back
CGPoint point = [[yourCGPointsArray objectAtIndex:0] CGPointValue];
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • Yes I've already done this. What I want to happen is that every time the touches ended, all the recorded CGPoints will be saved in database with a single identifier. Then, once the touches moved and ended again, the recorded points will be stored to the database with a different identifier. Have any idea how to do that? – Anna Fortuna Jun 18 '12 at 09:10
  • Just use the above method and store each array created into an NSDictionary. The Dictionary will store all the arrays and you can use an NSNumber or NSDate to "identify" each NSArray. You can get the last object of the Dictionary and increment by one (assuming you have used NSNumber for the identifier - you just cast as an NSInteger, increment and cast back). Lots of different ways to create an "identifier". – LJ Wilson Jun 18 '12 at 09:25
  • My problem is that the array is not "refreshed". I mean I want the array to empty out and record a new set of points every time the touchesMoved is called. That is the purpose of the "identifier", for me to know that the certain array of points are separated from another array of points. I hope it make sense though. – Anna Fortuna Jun 18 '12 at 09:35
  • Ok then, just have that NSMutableDictionary (original comment should have reflected that) contain only one object and just remove the object (NSArray) before adding a new object (NSArray) with a different (or the same) identifier (the Key). – LJ Wilson Jun 18 '12 at 10:18