Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2008, 04:03 AM
Administrator
 
Join Date: Jun 2008
Location: Arizona
Posts: 395
Send a message via AIM to mxweas Send a message via Yahoo to mxweas Send a message via Skype™ to mxweas
Default Easy UIView Animations! Frame, Alpha and more!

Some nice functionality UIView has, is the ability to animate different properties of UIView's and their subclass's. The basic idea is to tell UIView to setup for animations, then change a property or two on any number of views, and finish up the animation. Heres a quick example of animating a view to slide in from the right side of the screen.

Code:
CGRect rect = [ [ UIScreen mainScreen ] applicationFrame ];
rect.origin.x = rect.size.width; // move the rect off the screen.
UIView *myViewIWantToAnimate = [ [ UIView alloc ] initWithFrame: rect ]; // Initialize the view off to the right of the screen.

[ UIView beginAnimations: nil context: @"some-identifier-used-by-a-delegate-if-set" ]; // Tell UIView we're ready to start animations.
[ UIView setAnimationDelegate: self ]; // Set the delegate (Only needed if you need to use the animationDid... selectors)
[ UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:) ]; // example of a selector called with context when animation finishes.
[ UIView setAnimationCurve: UIViewAnimationCurveEaseInOut ];
[ UIView setAnimationDuration: 0.4f ]; // Set the duration to 4/10ths of a second.

CGRect tempFrame = myViewIWantToAnimate.frame; // Get the current frame.
tempFrame.origin.x = 0.0f // Move the view completely on screen.
myViewThatIWantToAnimate.frame = tempFrame; // set the new frame

[ UIView commitAnimations ]; // Animate!
Thats it!, you can animate multiple views at the same time, and this method works with almost all UIView properties.

Max
Reply With Quote
  #2 (permalink)  
Old 07-10-2008, 06:06 PM
Junior Member
 
Join Date: Jul 2008
Posts: 13
Default

UIView animations and transformations are fun. I have been looking for a way to stagger animations, do you think having a reoccurring thread and a animation action stack is the way to go, or is there something built into Cocoa Touch?
Reply With Quote
  #3 (permalink)  
Old 07-11-2008, 02:49 PM
optimo's Avatar
Junior Member
 
Join Date: Jun 2008
Posts: 9
Default

something with timers...
or performSelectorAfterDelay
Reply With Quote
  #4 (permalink)  
Old 08-23-2008, 08:46 PM
Administrator
 
Join Date: Jun 2008
Location: Arizona
Posts: 395
Send a message via AIM to mxweas Send a message via Yahoo to mxweas Send a message via Skype™ to mxweas
Default

If you wanted to do something like animate a view down and back up you could do something like this:

Code:
CGRect rect = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f);
UIView *myViewIWantToAnimate = [ [ UIView alloc ] initWithFrame: rect ]; // Make the view.
myViewIWantToAnimate.backgroundColor = [ UIColor blueColor ]; // Set the background color blue.

/* Animate the view down 60 pixels */
[ UIView beginAnimations: nil context: nil ]; // Tell UIView we're ready to start animations.
[ UIView setAnimationCurve: UIViewAnimationCurveEaseInOut ];
[ UIView setAnimationDuration: 1.0f ]; // Set the duration to 1 second.
myViewIWantToAnimate.frame = CGRectMake(rect.origin.x, rect.origin.y + 60.0f, rect.size.width, rect.size.height); // Set the frame 60 pixels down.
[ UIView commitAnimations ]; // Animate!

/* Animate the view back to it's original spot on the screen */
[ UIView beginAnimations: nil context: nil ]; // Tell UIView we're ready to start animations.
[ UIView setAnimationCurve: UIViewAnimationCurveEaseInOut ];
[ UIView setAnimationDuration: 1.0f ]; // Set the duration to 1 second.
myViewIWantToAnimate.frame = rect; // Set the frame to the original frame.
[ UIView commitAnimations ]; // Animate!
Max
Reply With Quote
  #5 (permalink)  
Old 11-18-2008, 11:55 AM
Junior Member
 
Join Date: Oct 2008
Posts: 1
Default

I'd spent two hours trying to reposition a frame with UIView's animation until I realized that this code won't work:

Code:
[UIView beginAnimations:nil context:NULL];
myView.frame.origin.y = aNewOriginY;
[UIView commitAnimations];
So, to remedy others from wasting their time, I'd like to point out that the property to be changed should always be set AS A WHOLE to get the transition work:

Code:
[UIView beginAnimations:nil context:NULL];
CGRect newFrame = myView.frame;
newFrame.origin.y = aNewOriginY;
myView.frame = newFrame;
[UIView commitAnimations];
Hope someone will find this useful.
Reply With Quote
Reply

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


All times are GMT. The time now is 05:30 AM.