我想让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);
}
}
答案 0 :(得分:0)
touchesBegan:
,如果用第二根手指触摸屏幕,则会再次调用它。
您应该查看touchesEnded:
和touchesCancelled:
答案 1 :(得分:0)
我想我必须拼出来:
nTouchTimegapArray = [NSMutableArray arrayWithCapacity:count];
[nTouchTimegapArray addObject:[NSNumber numberWithFloat: nTouchTimegap]];
该序列在每次迭代时有效地擦除数组。在阵列中没有可能有多个条目。
将第一行移出循环,并且,如果要累积来自方法的所有调用的触摸,请将其移至init
例程或viewDidLoad
或某些此类。