So I create an UIImageView programmatically and I place it on an array. In touchesMoved I set the x-position of the UIImageView to the x-position of the touch.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIImageView *b in _blocks) {
if ( b.image != wall) {
movingimage = b;
movingimage.userInteractionEnabled = YES;
NSLog(@"touch");
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint location = [touch locationInView:touch.view];
CGPoint xlocation = CGPointMake(location.x, movingimage.center.y);
movingimage.center = xlocation;
}
This code works fine if instead of using a UIImageView created programmatically I use one created in Interface Builder. But when I use code to create the UIImageView and tochesBegan starts at the UIImageView the touch coordinate from touchesMoved goes crazy and the imageView flashes really fast between 2 places.
Thanks for reading.
I guess this is because you are getting the touch point from a view that you are then relocating. So the next event will be "incorrect". I think that the best you could do is to capture the touch location from the superview.
EDIT:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint location = [touch locationInView:[movingimage superview]];
CGPoint xlocation = CGPointMake(location.x, movingimage.center.y);
movingimage.center = xlocation;
}