我正在尝试将 UITouches 存储在字典或数组中并且遇到了一些麻烦。存储 CGPoints 工作正常,但存储 UITouches 不行。
进一步说明:在触摸开始时初始化数组,将每个UITouch存储在一个数组中,当触摸结束时我想输出数组。我一直在寻找一段时间,但我还没有找到任何示例代码来执行此操作。
NSMutableArray *touchesArray;
NSMutableArray *pointArray;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
touchesArray = [[NSMutableArray alloc] init];
pointArray = [[NSMutableArray alloc] init];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
[pointArray addObject:[NSValue valueWithCGPoint: currentPoint]];
[touchesArray addObject:[NSValue valueWithPointer:(__bridge const void *)(touch)]];
for (UITouch *touch in touches)
{
NSLog(@"x location %f",[touch locationInView:self].x);
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches)
{
NSLog(@"%f",[touch locationInView:self].x);
}
for(id p in pointArray){
NSValue *val = p;
CGPoint point = [val CGPointValue];
NSLog(@"%f",point.x);
}
//ERROR!
for(id p in touchesArray){
NSValue *val = p;
UITouch *t = (UITouch*) p;
NSLog(@"%@",[t timestamp]);
}
}