2012-12-16 43 views
0

我要让NSMutableArray存储触摸时间上的差距,而是跨越一个问题来了:
NSMutableArray的计数只1。 我能做什么?一旦你触摸屏幕的NSMutableArray对象值相同的值

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

} 
+0

插入更多元素。 –

+2

而且你也可以在循环的外部创建数组,所以你不要在每次迭代中都替换旧数组。 (提示:Objective-C是*程序*语言。) –

回答

0

touchesBegan:将被调用,如果您触摸屏幕与第二手指会被再次调用。

你应该touchesEnded:touchesCancelled:

+0

NSMutableArray只返回相同的对象。这里是日志。 2012年12月17日03:46:54.464 MTAS [13411:10703] nTouchTimegapArray:1.3125 2012-12-17 03:46:54.464 MTAS [13411:10703] nTouchTimegapArray:1.3125 2012-12-17 03:46: 54.464 MTAS [13411:10703] nTouchTimegapArray:1.3125 2012-12-17 03:46:54.464 MTAS [13411:10703] nTouchTimegapArray:1.3125 2012-12-17 03:46:54.464 MTAS [13411:10703] --- ---------- –

+0

我认为有问题 for(int i = 0; i

0

我想我必须拼出来看看:

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

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

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

+0

绝对正确,但我得到的印象是OP需要这样做的正确方式的一个例子,否则他不会首先问这个问题和/或您的评论足够了。 :) – lnafziger

+0

NSMutableArray的对象日志只有一个值。我想看到NSMutableArray的所有值(对象)。 T.T –

+0

@lnafziger - 我不认为他知道他想要什么。 –

相关问题