0

我想NSMutableArray存储触摸时间间隔,但遇到了一个问题:
NSMutableArray计数仅为1. 我能做些什么?

NSInteger count = 0;
float firstTempTime = 0;
float nTempTime = 0;
float nTouchTimegap = 0;

#pragma mark –
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
messageLabel.text = @"Touches Began"; 
[self updateLabelsFromTouches:touches];  
NSArray *array = [touches allObjects];   
for (UITouch *touch in array){
    count = count +1;
    NSLog(@"began touch count: %d", count);

    nTempTime = [touch timestamp];
    NSLog(@"n TempTime stamp : %lf", nTempTime); 

    if (count == 1) {
        firstTempTime = [touch timestamp];
    }

    else {
        nTouchTimegap = nTempTime - firstTempTime;
        firstTempTime = nTempTime;
    }
    NSLog(@"nTouchTimegap : %lf", nTouchTimegap);

    nTouchTimegapArray = [NSMutableArray arrayWithCapacity:count];
    [nTouchTimegapArray addObject:[NSNumber numberWithFloat: nTouchTimegap]];


    for(id obj in nTouchTimegapArray){
        NSLog(@"nTouchTimegapArray : %i", [nTouchTimegapArray count]);
        NSLog(@"nTouchTimegapArray : %@", obj);
    }

}
4

2 回答 2

0

我想我必须把它拼出来:

nTouchTimegapArray = [NSMutableArray arrayWithCapacity:count];
[nTouchTimegapArray addObject:[NSNumber numberWithFloat: nTouchTimegap]];

该序列在每次迭代时有效地擦除了数组。数组中不可能有多个条目。

将第一行移出循环,并且,如果您想从方法的所有调用中累积触摸,请将其移至init例程或viewDidLoad类似的例程中。

于 2012-12-16T20:44:24.853 回答
0

touchesBegan:一旦你触摸屏幕就会被调用,如果你用第二根手指触摸屏幕,它会再次被调用。

你应该看看touchesEnded:touchesCancelled:

于 2012-12-16T19:54:36.317 回答