Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-04-2011, 11:26 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 60
nathanl1192 is on a distinguished road
Default Make image move, or change when pressing button.

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
nathanl1192 is offline   Reply With Quote
Old 04-04-2011, 11:41 AM   #2 (permalink)
Registered Member
 
spentak's Avatar
 
Join Date: Mar 2010
Posts: 63
spentak is on a distinguished road
Default

Quote:
Originally Posted by nathanl1192 View Post
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:

Code:
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];
                         }
                     }];

Last edited by spentak; 04-04-2011 at 01:43 PM.
spentak is offline   Reply With Quote
Old 04-04-2011, 12:17 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 60
nathanl1192 is on a distinguished road
Default

Quote:
Originally Posted by spentak View Post
You could put this in a method and call it when the button is tapped:

Code:
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
Code:
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.
nathanl1192 is offline   Reply With Quote
Old 04-04-2011, 12:58 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

I think you're supposed to pass the extensions as well in imageNamed method.
baja_yu is offline   Reply With Quote
Old 04-04-2011, 01:45 PM   #5 (permalink)
Registered Member
 
spentak's Avatar
 
Join Date: Mar 2010
Posts: 63
spentak is on a distinguished road
Default

Quote:
Originally Posted by nathanl1192 View Post
This doesn't seem to work. Gets stuck on
Code:
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
spentak is offline   Reply With Quote
Old 04-04-2011, 01:56 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 60
nathanl1192 is on a distinguished road
Default

Quote:
Originally Posted by spentak View Post
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:

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 (
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:

Code:
[self moveBackAnimation];
Thanks so far!
nathanl1192 is offline   Reply With Quote
Old 04-04-2011, 02:04 PM   #7 (permalink)
Registered Member
 
spentak's Avatar
 
Join Date: Mar 2010
Posts: 63
spentak is on a distinguished road
Default

Quote:
Originally Posted by nathanl1192 View Post
I know, I did use that. Here is my code:

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 (
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:

Code:
[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];
spentak is offline   Reply With Quote
Old 04-04-2011, 02:16 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 60
nathanl1192 is on a distinguished road
Default

Quote:
Originally Posted by spentak View Post
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:delayptions:animations:'" and " Method '+animatewithduration:delayptions:animations:' not found (return type defaults to id)"

My overall code is:
Code:
-(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;
                     }
     ];
nathanl1192 is offline   Reply With Quote
Old 05-05-2011, 11:57 AM   #9 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 60
nathanl1192 is on a distinguished road
Default Add delay to event

Ignore this post.

Last edited by nathanl1192; 05-05-2011 at 11:57 AM. Reason: Wrong post sorry.
nathanl1192 is offline   Reply With Quote
Reply

Bookmarks

Tags
animate, image, move, xcode 4

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 367
12 members and 355 guests
condor304, dansparrow, dre, ilmman, LezB44, michelle, Objective Zero, samdanielblr, Sami Gh, shagor012, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:40 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0