将触摸笔划的坐标存储到NSMutableArray中

时间:2013-11-19 10:10:23

标签: objective-c nsmutablearray nsnumber uitouch

我正在尝试记录用户手指的移动并使用nsmutablearrays和nsnumbers将其存储为x和y值,我想在调用touchesEnded后立即使用nslog在控制台中显示值。

我哪里错了?

-(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--;
    }
}

1 个答案:

答案 0 :(得分:0)

你有一个错误的索引,a是数组中的点数,你从(a - 1)开始到0([xScreenLocations objectAtIndex:(a - 1)]而不是[xScreenLocations objectAtIndex:a]),你还必须纠正你在while循环(a > 0)而不是(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--;
    }
}