|
|||
|
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! Max |
|
|||
|
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?
|
|
|||
|
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! |
|
|||
|
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]; Code:
[UIView beginAnimations:nil context:NULL]; CGRect newFrame = myView.frame; newFrame.origin.y = aNewOriginY; myView.frame = newFrame; [UIView commitAnimations]; |
![]() |
| Thread Tools | |
| Display Modes | |
|
|