CABasic Animation in iPhone

Introduction

In this article I will create a Empty View application. Here to do animation we pass images using an array and set their view duration. But here we show the animation screen from the top to the bottom and vise versa. To create an animated view we write code for the appdelegate.m Objective-C class. To implement it here we add the <QuartzCore/QuartzCore.h> framework.

To understand it we use the following.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Empty View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on Create.

Step 6

Now we write the code.

AppDelegate.h

#import <UIKit/UIKit.h>
@interface testviewAppDelegate : UIResponder <UIApplicationDelegate>
@property (strongnonatomicUIWindow *window;
@end

AppDelegate.m

#import "testviewAppDelegate.h"
@implementation testviewAppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow allocinitWithFrame:[[UIScreen mainScreenbounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
NSArray *myimagearray = [NSArray arrayWithObjects:[UIImage imageNamed:@"Sachin.png"],[UIImageimageNamed:@"image2.jpg"],[UIImage imageNamed:@"image3.jpg"],[UIImage imageNamed:@"image3.jpg"],[UIImage imageNamed:@"image4.jpg"],nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(00320480)];
myAnimatedView.animationImages = myimagearray;
myAnimatedView.animationDuration = 1;
myAnimatedView.animationRepeatCount = 0;
[myAnimatedView startAnimating];
[self.window addSubview:myAnimatedView];
[myAnimatedView release];
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
theAnimation.duration=2;
theAnimation.repeatCount=10;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:0];
theAnimation.toValue=[NSNumber numberWithFloat:480];
[_window.layer addAnimation:theAnimation forKey:@"animateLayer"];
[self.window makeKeyAndVisible];
return YES;
@end

Step 7

Finally we click on the run button to show the output.

Step 8

Output 1 in iPhone:

Output1-in-iPhone.png

Output 2 in iPhone:

Output2-in-iPhone.png

Output 3 in iPhone:

Output3-in-iPhone.png

Output 4 in iPhone:

Output4-in-iPhone.png


Similar Articles