I'm trying to make a simple app where an image that is "pinned" ge's returned to its position after being moved by a finger. This is probably explained better with code:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
image.transform = CGAffineTransformIdentity;
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([image frame], [touch locationInView:nil]))
{
image.center = [touch locationInView:nil];
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (pin) {
CGPoint point = image.center;
CGPoint center = self.view.center;
//CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
image.transform = CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y);
//image.transform = CGAffineTransformConcat(image.transform, CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y));
[UIView commitAnimations];
}
}
Every time I press the image it shifts so that it moves out from under my finger. I think it has something to do with the transformations. Could anyone please point my in the right direction?
EDITED
I'd ACTUALLY do it like this:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint location = [[touches anyObject] locationInView:[touch view]];
CGPoint difference = CGPointMake(location.x - image.center.x, location.y - image.center.y);
image.transform = CGAffineTransformTranslate(image.transform, difference.x, difference.y);
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (pin) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
image.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
}