I’m building an interactive anagram feature at the moment for my iPad app and am trying to use touchmove events to get the selected item to follow the movement smoothly, but it is jumping for some weird reason.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];
if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
NSLog(@"start moving");
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];
CGPoint location = [touch locationInView:touch.view];
if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
NSLog(@"touch view centre (x,y) - (%f,%f)",touch.view.center.x,touch.view.center.y);
NSLog(@"location (x,y) - (%f,%f)",location.x,location.y);
touch.view.center = location;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];
if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]){
NSLog(@"Now stop moving!");
}
}
The above are my 3 touch methods and it would seem like they should be working fine. You can see in the touchesMoved method that I am checking to see whether the object being moved is of a specific class (a custom class I made for the same project). Has anyone any ideas why this might be happening? It is following the cursor in the simulator but it seems to snap to the bottom corner before it follows the cursor, so it is basically jumping all over the screen.
Any ideas?
I believe the problem is that you are working with the touch relative to the subview being touched.
will get you a location in reference to the your custom view’s parent.
In the touchesMoved method you can look at the displacement and apply the same displacement to your view