store touchesMoved value to NSArray in iPhone

2019-09-09 11:58发布

问题:

i want store my touchesMoved value in to NSArray how can i store that. now i try to store my touches moved value but i won't print value. here is my code

- (void)initWithSize:(int)nX height:(int)nY {
    xWidth = nX;
    yHeight = nY;

        xArray = [[NSMutableArray alloc] initWithCapacity:xWidth];

        for (int i = 0; i < xWidth; i++) {

            yArray = [[NSMutableArray alloc] initWithCapacity:yHeight];

            for (int j = 0; j < yHeight; j++) {                

            }

            roomArray = [[NSArray alloc] initWithArray:yArray];
            [xArray addObject:roomArray];
            [roomArray release];
            [yArray release];


        }

i call this method like this

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self];

    [self initWithSize:currentPoint.x height:currentPoint.y];

}

i print this value like this

   - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {   

        NSString *myArrayString = [xArray description];
          NSLog(@"screeX:%@ ",xArray);    
    }

but i did't get that pixels in my console output i got output like this

screeX:(
        (
    ),
        (
    ),
        (
    ),
        (
    ),
        (
    ),
        (
    ),
        (
    ),
        (

回答1:

The way you are implement is not proper.

  1. Declare a NSMutableArray object in .h file.

    NSMutableArray *_array;
    
  2. You can allocate the array in viewDidLoad.

    _array = [[NSMutableArray alloc] init];
    
  3. In touchesBegin, remove all the objects of the array.

    [_array removeAllObjects];
    
  4. In touchesMoved, add the CGPoints to the array as follows.

    [_array addObject:[NSValue valueWithCGPoint: currentPoint]]; 
    

Now , in touchesEnded, print the array and see the values.