How do you make an image follow your finger in objective-c? [closed]
How do you make an image follow your finger in objetive-c?
- 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:
alt text http://brockwoolf.com/shares/stackoverflow/3445494/screen.png
Download Xcode project (32kb)
there is an exellent post here.
by Divan Visagie
here is the relevant code (taken from the link above):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Find the path for the menu resource and load it into the menu array
NSString *menuPlistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
menuArray = [[NSArray alloc] initWithContentsOfFile:menuPlistPath];
//add some gestures
// UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
// [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
//[self.view addGestureRecognizer:swipeLeft];
// UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
// [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
//[self.view addGestureRecognizer:swipeRight];
}
float difference;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint contentTouchPoint = [[touches anyObject] locationInView:content];
difference = contentTouchPoint.x;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint pointInView = [[touches anyObject] locationInView:self.view];
float xTarget = pointInView.x - difference;
if(xTarget > menuTable.frame.size.width)
xTarget = menuTable.frame.size.width;
else if( xTarget < 0)
xTarget = 0;
[UIView animateWithDuration:.25
animations:^{
[content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
}
];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint endPoint = [[touches anyObject] locationInView:self.view];
float xTarget = endPoint.x - difference;
if(xTarget < (menuTable.frame.size.width/2))
xTarget = 0;
else
xTarget = menuTable.frame.size.width;
[UIView animateWithDuration:.25
animations:^{
[content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
}
];
}
The Apple Sample Code library comes with a well-written example, named Touches. It also demonstrates the new UIGestureRecognizers feature in iOS 4.0.
精彩评论