NsMutableArray对象值只是相同的值

时间:2012-12-16 19:23:58

标签: iphone ios5

我想让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);
    }

}

2 个答案:

答案 0 :(得分:0)

触摸屏幕后会调用

touchesBegan:,如果用第二根手指触摸屏幕,则会再次调用它。

您应该查看touchesEnded:touchesCancelled:

答案 1 :(得分:0)

我想我必须拼出来:

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

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

将第一行移出循环,并且,如果要累积来自方法的所有调用的触摸,请将其移至init例程或viewDidLoad或某些此类。