How do you make an image follow your finger in objetive-c?
+4
A:
- Create your UIView and configure it (or any subclass thereof such as a UIImageView)
- Set the position of your Image to be where the user is touching:
There are four delegate methods for accepting touch events which are a part of any class that inherits from UIResponder such as a UIView. Use the delegate method that is most appropriate to you. If you want it to follow your finger, this will be -touchesMoved:
- (void) touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:self];
myImageView.center = pt;
}
Other delegate methods available to you are:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
I wrote an example application that does exactly what you want. It's a demo of drawing Quartz 2D graphics, but it draws a red square and black circle where you drag your finger and should be simple enough to follow:
Download Xcode project (32kb)
Brock Woolf
2010-08-10 02:06:16
A:
The Apple Sample Code library comes with a well-written example, named Touches. It also demonstrates the new UIGestureRecognizers feature in iOS 4.0.
ohho
2010-08-10 04:32:29