user964627
user964627

Reputation: 665

Touch and drag an image around the screen

I have three images that I want the user to move around the screen. For example you have a football, baseball, and a basketball. I would like the user to select one of the images and then move that image around on the screen without affecting the other two. I watched some tutorials on make a image move around on the screen but what I am having a hard time moving around several images.

What is the best way to enable three images on the screen to have separate touch controls (Drag Around).

Upvotes: 1

Views: 5845

Answers (1)

Alex Nichol
Alex Nichol

Reputation: 7510

Simply create a UIImageView subclass, for this example I'll call it DragView, and use this class for each of the image views:

DragView.h

@interface DragView : UIImageView {
}
@end

DragView.m

- (void)touchesMoved:(NSSet *)set withEvent:(UIEvent *)event {
    CGPoint p = [[set anyObject] locationInView:self.superview];
    self.center = p;
}

Now, the user will be able to drag any individual instance of DragView around in its superview. If you are creating your image views manually (e.g. imageView = [[UIImageView alloc] initWithImage:anImage]), replace UIImageView with DragView. You will also need to import DragView.h in your view controller's header.

If you are initializing your image views through interface builder, you can use the attributes inspector to change the class of the image view from UIImageView to DragView.

Upvotes: 6

Related Questions