I’m trying to store UITouches in a dictionary or array and having some trouble. Storing CGPoints works fine, but storing UITouches not.
Further explanation: initializing array when touches begin, store each UITouch in an array, when touches end I want to output the array. I have been looking for some time, but I haven’t found any sample code to do this.
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]);
}
}
Using a wrapper object worked for me. UITouch always pointed to the same adress.