Tuesday, February 8, 2011

uiview aniamtion



- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    //  Create the application's window and set its background color.
    CGRect windowRect = [[UIScreen mainScreen] applicationFrame];
    UIWindow *window = [[UIWindow alloc] initWithFrame:windowRect];
    [window setBackgroundColor:[UIColor lightGrayColor]];
    
    [self setWindow:window];
    
    //  Create an empty view with a different background color so we can
    //  see something on the screen. The four float constants define the
    //  x, y, width, and height of the view's frame rectangle.
    //
    CGRect viewRect1 = { 80.0, 200.0, 160.0, 100.0 };
    WiggleView *wiggleView1 = [[WiggleView alloc] initWithFrame:viewRect1];
    [wiggleView1 setBackgroundColor:[UIColor darkGrayColor]];
    
    //  Create another view half the size of the first one with a 
    //  different background color.
    //
    CGRect viewRect2 = { 0.0, 340.0, 80.0, 50.0 };
    WiggleView *wiggleView2 = [[WiggleView alloc] initWithFrame:viewRect2];
    [wiggleView2 setBackgroundColor:[UIColor purpleColor]];
    
    //  Add the two views to the subviews array so that wiggleView1 is on top 
    //  of wiggleView2
    //
    [window addSubview:wiggleView2];
    [window addSubview:wiggleView1];

    [window makeKeyAndVisible];
    
    //  Send wiggle and fly messages to the subviews
    //
    [wiggleView1 wiggle];
    [wiggleView2 fly];

    
    [window release];
    [wiggleView1 release];
    [wiggleView2 release];
}




//////////////////////////////////////////////////////////////////////////////
#import "WiggleView.h"
#import <QuartzCore/QuartzCore.h>

@implementation WiggleView

//  These compiler directives add some nice formatting to the Xcode dropdown
//  above (which should read "@implementation WiggleView").
//  The first directive causes the menu to draw a line; the second, causes it 
//  to add the heading, "Transform and Animate."
//
#pragma mark -
#pragma mark Transform and Animate

//  Make the view wiggle.!
//
- (void)wiggle
{
    // Start of animation block.
    [UIView beginAnimations:nil context:NULL];
    
    // Animation settings.
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatCount:12.5];
    [UIView setAnimationRepeatAutoreverses:YES];
    
    // Transformation. Rotates the view .075 radians.
    [self setTransform:CGAffineTransformMakeRotation(-0.75)];
    
    // End of animation block -- commit the animations.
    [UIView commitAnimations];
}

//  Mke the view fly.
//
- (void)fly
{
    // Start animation block.
    [UIView beginAnimations:nil context:NULL];
    
    // Animation settings.
    [UIView setAnimationDuration:1.75];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    
    //  Transformations. Translate by 200 pixels on the x axis and -300 pixels
    //  on the y axis, and scale down from twice its original size. Note that
    //  because these are matrix transformations, order matters here.
    //
    [self setTransform:CGAffineTransformMakeScale(2.0, 2.0)];
    //[self setTransform:CGAffineTransformMakeTranslation(200.0, -300.0)];
    
    // End animation block.
    [UIView commitAnimations];    
}

@end
////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment

Followers