Moving Rectangle in iPhone

In this application we will see how to Rectangle moving in the application . We are using here CoreGraphics.framework , for animation behavior. So just have a look.

In this application we will see how to Rectangle moving in the application . We are using here CoreGraphics.framework , for animation behavior. So  just have a look.

Step 1: Create a Window base application using template. Give the application name “ChangeRect”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add here UIView class to the project.Choose New file -> Select cocoa touch classes group and then select UIView subclass . Give the name “CreateRectView”.

Step 4: open the AppDelegate.m file, we create a background and set the gray background color and make two different rectangle with different color. So make the following changes in the file:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  CGRect windowRect = [[UIScreen mainScreen] applicationFrame];
  UIWindow *window = [[UIWindow alloc] initWithFrame:windowRect];
  [window setBackgroundColor:[UIColor greenColor]];

  [self setWindow:window];

  CGRect changeRect1 = { 80.0, 100.0, 160.0, 100.0 };
  ChangeRectView *changeView1 = [[ChangeRectView alloc]   initWithFrame:changeRect1];
  [changeView1 setBackgroundColor:[UIColor darkGrayColor]];

  CGRect changeRect2 = { 80.0, 300.0, 120.0, 100.0 };
  ChangeRectView *changeView2 = [[ChangeRectView alloc]    initWithFrame:changeRect2];
  [changeView2 setBackgroundColor:[UIColor redColor]];

  [window addSubview: changeView2];
  [window addSubview: changeView1];

  [window makeKeyAndVisible];

  [changeView1 ChangeView];
  [changeView2 fly];

  [window release];
  [changeView1 release];
  [changeView2 release];
}

Step 5: In the ChangeRectView.h file, we define two methods. Make the following changes in the file:

@interface ChangeRectView : UIView {
}
  - (void)ChangeView;
  - (void)fly;

Step 6: Now open the ChangeRectView.m file and make the changes:

- (void)ChangeView
{
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.25];
  [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  [UIView setAnimationRepeatCount:10];
  [UIView setAnimationRepeatAutoreverses:YES];
  [self setTransform:CGAffineTransformMakeRotation(-0.75)];
  [UIView commitAnimations];

}
- (void)fly
{
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.25];
  [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  [UIView setAnimationRepeatCount:10];
  [UIView setAnimationRepeatAutoreverses:YES];
  [self setTransform:CGAffineTransformMakeRotation(-0.75)];
  [UIView commitAnimations];
}

Step 7: In the main.m file, we need to provide the name of the AppDelegate class as a argument here. So make the following changes in the file:

int main(int argc, char *argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  int retVal = UIApplicationMain(argc, argv, nil, @"ChangeRectAppDelegate");

  [pool release];
  return retVal;
}

Step 8: Now compile and run the application in the simulator.

You can downloaded SourceCode from here ChangeRect 2

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG