Reputation: 12351
I'm quite new to Objective-C and am looking to create a fairly simple Jigsaw. I'm currently just looking at making a 4-piece puzzle as a way of learning the method.
At the moment I have 4 pieces placed around the screen that when dragged to a specific place on screen, snap to that place.
My problem is trying to animate the touches. I want the image touched to expand a little but i'm having trouble with the syntax. I've looked at the MoveMe example that Apple provide but it's not helping me in this situation. Below is the code. I'd appreciate any help with this. Thanks.
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == image1) {
image1.center = location;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
image1.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView commitAnimations];
} else if ([touch view] == image2) {
image2.center = location;
animateFirstTouch:image2;
} else if ([touch view] == image3) {
image3.center = location;
image3.transform = CGAffineTransformMakeScale(1.5, 1.5);
} else if ([touch view] == image4) {
image4.center = location;
image4.transform = CGAffineTransformMakeScale(1.5, 1.5);
}
}
-(void) animateFirstTouch:(UIView *)image {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.view.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView commitAnimations];
}
The code for image1 works, and the image is scaled up over 0.5 seconds.
The code for image2 throws up an error: expression result unused for animateFirstTouch:image2.
The code for image3 and image4 scales up the images without the animation.
Upvotes: 0
Views: 1077
Reputation: 19469
Refer to Kuldeep's answer from this link. I think that would work for you:
iPhone/iOS: How to implement Drag and Drop for subviews of a Scrollview?
Also as an alternative refer to iPhone's answer in this link:
iPhone App: implementation of Drag and drop images in UIView
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == image1) {
[self animateFirstTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
[self animateFirstTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
[self animateFirstTouch:image3 withLocation:location];
} else if ([touch view] == image4) {
[self animateFirstTouch:image4 withLocation:location];
}
}
-(void) animateFirstTouch:(UIImageView *)image withLocation:(CGPoint)location {
image.center = location;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
image.transform = CGAffineTransformMakeScale(1.5, 1.5);
[UIView commitAnimations];
}
Hope this helps you.
Contact me if you need more help.
Upvotes: 1