0

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.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
garethdn
  • 12,022
  • 11
  • 49
  • 83

1 Answers1

1

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.

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • Hi. Thanks but that seems like a a different approach altogether to the one i really want. I'm trying to learn about animations and scaling. I have the animation function set up as far as i can see, i'm just having trouble passing the image to the function. If you can help with this it would be great. – garethdn Mar 24 '12 at 12:49
  • @garethdn: Please check the code I have edited my answer and modified your code a bit and added it to my answer.Please use that code. Please contact me if you want some help about it. – Parth Bhatt Mar 24 '12 at 15:15
  • Thanks for that Parth. Is there any way to confine this animation to it's own method, like the animateFirstTouch function i've written above. It seems wasteful to be repeating the same code over and over. Or even better, is there a way to say: if the object being touched is an image, animate it to scale up over 0.5 seconds? – garethdn Mar 24 '12 at 17:56
  • @garethdn I have edited the answer and added the modified code. Also check `animateFirstTouch: withLocation:` method and Also I have modified code under the `touchesBegan`. Please check my answer. I have made edited the answer. Please use the code which I have posted in my answer. Please contact me if you want more help on this. Thanks – Parth Bhatt Mar 24 '12 at 19:32
  • Thanks for our continued help Parth. I'm getting a few errors with the above code. For [self animateFirstTouch:imageXXX withLocation:location]; i get the error "Incompatible pointer types sending "UIImageView *_weak' to parameter of type 'UIImage *'. There is another error in the method: "Property "center" not found on object of type "UIImage *". – garethdn Mar 24 '12 at 19:59
  • @garethdn Oh I am Sorry, use this as your method definition: `-(void) animateFirstTouch:(UIImageView *)image withLocation:(CGPoint)location`. Here I have replaced `(UIImage *)` with `(UIImageView *)`. Hope you get the change. Contact me if you need more help. Thanks – Parth Bhatt Mar 24 '12 at 20:01
  • @garethdn You are welcome bro. :) You are from which country? – Parth Bhatt Mar 24 '12 at 20:07