I am trying to drag a UIImageView around the iphone screen in my application.
Currently, the drag and drop function that I set up is good, and dragging an image moves it around the screen, the problem is that you don’t have to drag the image to move it, you can also drag anywhere on the screen and it will move the image. I am new to this platform, so I can’t think about what to do to solve this problem. My current code to drag and drop the image:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:pig];
startLocation = pt;
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:pig];
CGRect frame = [pig frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[pig setFrame: frame];
}
Any help appreciated. By the way, the pig is UIImageView. In addition, I noticed that if I set the user’s interaction with the “pig” image to turn on the image, it no longer drags, but when it is not installed, it is.