Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Make image move, or change when pressing button.

nathanl1192nathanl1192 Posts: 61Registered Users
edited May 2011 in iPhone SDK Development
Hi there,

I need to make an image move around (smoothly) when a button is tapped. How do I do this? Also, when a button is pressed, how do I change an the image, for a fixed amount of time, (say 5 seconds) then it changes back automatically.

Thanks, any code snippets or comprehensive tutorials will be appreciated.

Nathan :)

-I am using Xcode 4
Post edited by nathanl1192 on

Replies

  • spentakspentak Posts: 63Registered Users
    edited April 2011
    nathanl1192;319537 said:
    Hi there,

    I need to make an image move around (smoothly) when a button is tapped. How do I do this? Also, when a button is pressed, how do I change an the image, for a fixed amount of time, (say 5 seconds) then it changes back automatically.

    Thanks, any code snippets or comprehensive tutorials will be appreciated.

    Nathan :)

    -I am using Xcode 4
    You could put this in a method and call it when the button is tapped:

    CGRect theMoveToPosition = CGRectMake(50, 50, myImage.frame.size.width, myImage.frame.size.height);
    [UIView animateWithDuration:5 delay:0 options:UIViewAnimationCurveEaseInOut
    animations:^ {
    myImage.image = [UIImage imageNamed:@\"MyNewImage.png\"];
    myImage.frame = theMoveToPosition;
    }completion:^(BOOL finished) {
    if (finished) {
    myImage.image = [UIImage imageNamed:@\"FirstImage.png\"];
    [self moveBackAnimation];
    }
    }];
  • nathanl1192nathanl1192 Posts: 61Registered Users
    edited April 2011
    spentak;319545 said:
    You could put this in a method and call it when the button is tapped:

    CGRect theMoveToPosition = CGRectMake(50, 50, myImage.frame.size.width, myImage.frame.size.height);
    [UIView animateWithDuration:5 delay:0 options:UIViewAnimationCurveEaseInOut
    animations:^ {
    myImage.image = [UIImage imageNamed:@\"MyNewImage\"];
    myImage.frame = theMoveToPosition;
    }completion:^(BOOL finished) {
    if (finished) {
    myImage.image = [UIImage imageNamed:@\"FirstImage\"];
    [self moveBackAnimation];
    }
    }];
    This doesn't seem to work. Gets stuck on
    myImage.image = [UIImage imageNamed:@\"FirstImage\"];
    because it is a button I am moving. I have tried changing to UIButton and myImage.button but no luck. (I have no idea if that was a good thing to do).

    Any suggestions, also, what about changing the image?

    Thanks again.
  • baja_yubaja_yu Posts: 6,175Super Moderators, Registered Users
    edited April 2011
    I think you're supposed to pass the extensions as well in imageNamed method.
  • spentakspentak Posts: 63Registered Users
    edited April 2011
    nathanl1192;319557 said:
    This doesn't seem to work. Gets stuck on
    myImage.image = [UIImage imageNamed:@\"FirstImage\"];
    because it is a button I am moving. I have tried changing to UIButton and myImage.button but no luck. (I have no idea if that was a good thing to do).

    Any suggestions, also, what about changing the image?

    Thanks again.
    Yeah you need to add the file extension in the string (I omitted them in my example). So [UIImage imageNamed:@"FirstImage.png"];

    .png or .jpeg or whatever you are using
  • nathanl1192nathanl1192 Posts: 61Registered Users
    edited April 2011
    spentak;319589 said:
    Yeah you need to add the file extension in the string (I omitted them in my example). So [UIImage imageNamed:@"FirstImage.png"];

    .png or .jpeg or whatever you are using
    I know, I did use that. Here is my code:

    -(IBAction)pushbikemove {

    CGRect theMoveToPosition = CGRectMake(50, 50, bike.frame.size.width, bike.frame.size.height);
    [UIView animateWithDuration:5 delay:0 options:UIViewAnimationCurveEaseInOut
    animations:^ {
    bike.image = [UIImage imageNamed:@\"bike.png\"];
    bike.frame = theMoveToPosition;
    }completion:^(BOOL finished) {
    if (finished) {
    bike.image = [UIImage imageNamed:@\"bike.png\"];
    [self moveBackAnimation];
    }
    }];

    }


    I am getting errors about bike.image such as "Property 'image' not found on object of type 'UIButton*'" and "Request for Member 'image' in something not a structure or union".

    I tried deleting those lines of code (
    bike.image = [UIImage imageNamed:@\"bike.png\"];

    and

    bike.image = [UIImage imageNamed:@\"bike.png\"];


    ) just to see what would happen, and the image then moved, but did not move back, as the application crashed and pointed me to:

    [self moveBackAnimation];


    Thanks so far!
  • spentakspentak Posts: 63Registered Users
    edited April 2011
    nathanl1192;319592 said:
    I know, I did use that. Here is my code:

    -(IBAction)pushbikemove {

    CGRect theMoveToPosition = CGRectMake(50, 50, bike.frame.size.width, bike.frame.size.height);
    [UIView animateWithDuration:5 delay:0 options:UIViewAnimationCurveEaseInOut
    animations:^ {
    bike.image = [UIImage imageNamed:@\"bike.png\"];
    bike.frame = theMoveToPosition;
    }completion:^(BOOL finished) {
    if (finished) {
    bike.image = [UIImage imageNamed:@\"bike.png\"];
    [self moveBackAnimation];
    }
    }];

    }


    I am getting errors about bike.image such as "Property 'image' not found on object of type 'UIButton*'" and "Request for Member 'image' in something not a structure or union".

    I tried deleting those lines of code (
    bike.image = [UIImage imageNamed:@\"bike.png\"];

    and

    bike.image = [UIImage imageNamed:@\"bike.png\"];


    ) just to see what would happen, and the image then moved, but did not move back, as the application crashed and pointed me to:

    [self moveBackAnimation];


    Thanks so far!
    I used [self moveBackAnimation] as an example. You could define a method named -(void)moveBackAnimation - which basically has animation code (like the code I pasted) to move the animation back to where you want it to go.

    Also to set your button image you could do: [bike setImage:[UIImage imageNamed:@"bike.png"] forState:UIControlStateNormal];
  • nathanl1192nathanl1192 Posts: 61Registered Users
    edited April 2011
    spentak;319594 said:
    I used [self moveBackAnimation] as an example. You could define a method named -(void)moveBackAnimation - which basically has animation code (like the code I pasted) to move the animation back to where you want it to go.

    Also to set your button image you could do: [bike setImage:[UIImage imageNamed:@"bike.png"] forState:UIControlStateNormal];
    Thanks for that! That seems to almost work.

    I am now getting errors in my -(void) action. I am getting told: "UIView may not respond to '+animatewithduration:delay:options:animations:'" and " Method '+animatewithduration:delay:options:animations:' not found (return type defaults to id)"

    My overall code is:
    -(IBAction)pushbikemove {

    CGRect theMoveToPosition = CGRectMake(50, 50, bike.frame.size.width, bike.frame.size.height);
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationCurveEaseInOut
    animations:^ {

    bike.frame = theMoveToPosition;
    }completion:^(BOOL finished) {
    if (finished) {

    [self pushbikemoveback];
    }
    }];
    }

    -(void)pushbikemoveback {

    CGRect theMoveToPosition = CGRectMake(6, 39, bike.frame.size.width, bike.frame.size.height);
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationCurveEaseInOut
    animations:^ {

    bike.frame = theMoveToPosition;
    }
    ];
  • nathanl1192nathanl1192 Posts: 61Registered Users
    edited May 2011
Sign In or Register to comment.