Question

I am trying to record the movement of the users finger and store it as x and y values using nsmutablearrays and nsnumbers, i want to display the values in the console using nslog as soon as touchesEnded is called.

where am i going wrong ?

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint Location = [touch locationInView:self.view];
    [xScreenLocations addObject:[NSNumber numberWithFloat:Location.x]];
    [yScreenLocations addObject:[NSNumber numberWithFloat:Location.y]];
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    int a = (int)[xScreenLocations count];
    while  (a >= 0) 
    {
        NSNumber *currentXNumber = [xScreenLocations objectAtIndex:a];
        float currentXInt = currentXNumber.floatValue;
        NSNumber *currentYNumber = [yScreenLocations objectAtIndex:a];
        float currentYInt = currentYNumber.floatValue;
        NSLog(@"%.1f  %.1f",currentXInt,currentYInt);
        a--;
    }
}
Was it helpful?

Solution

You have a wrong index, a is the number of point in the array, you have start from (a - 1) to 0([xScreenLocations objectAtIndex:(a - 1)] instead of [xScreenLocations objectAtIndex:a]), you also have to correct you condition in the while loop (a > 0) instead of (a >= 0).

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    int a = (int)[xScreenLocations count];
    while  (a > 0) 
    {
        NSNumber *currentXNumber = [xScreenLocations objectAtIndex:(a - 1)];
        float currentXInt = currentXNumber.floatValue;
        NSNumber *currentYNumber = [yScreenLocations objectAtIndex:(a - 1)];
        float currentYInt = currentYNumber.floatValue;
        NSLog(@"%.1f  %.1f",currentXInt,currentYInt);
        a--;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top