UIView animation settings, uiview Animation
Create a traffic light, and the red and green colors change once every five seconds. The area of the lights (color blocks) must also increase. Yellow lights shine for 3 seconds (0.9 seconds, 0.1 seconds), click the button to start running.
//AppDelegate.h#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end
// AppDelegate. m // AppDelegate. m // traffic light /// Created by apple on 15/4/9. // Copyright (c) 2015 hecheng. all rights reserved. // # import "AppDelegate. h "@ interface AppDelegate () {UIView * _ redView; UIView * _ greenView; UIView * _ yellowView; CGFloat x; CGFloat y;} @ end @ implementation AppDelegate-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// calculate the screen size UIScreen * screens = [UIScreen mainScreen]; CGRect rect = screens. bounds; // calculates the center position of the screen x = rect. size. width/2; y = rect. size. height/2; self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds]; self. window. backgroundColor = [UIColor whiteColor]; [self. window makeKeyAndVisible]; // set the red light. The initial color is black _ redView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 50, 50)]; _ redView. backgroundColor = [UIColor blackColor]; [self. window addSubview: _ redView]; _ redView. center = CGPointMake (X-100, y); // set the green light, the initial color is black _ greenView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 50, 50)]; _ greenView. backgroundColor = [UIColor blackColor]; [self. window addSubview: _ greenView]; _ greenView. center = CGPointMake (x, y); // set the yellow light. The initialization color is black _ yellowView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 50, 50)]; _ yellowView. backgroundColor = [UIColor blackColor]; [self. window addSubview: _ yellowView]; _ yellowView. center = CGPointMake (x + 100, y); // set the button UIButton * btn = [UIButton buttonWithType: UIButtonTypeSystem]; [btn setTitle: @ "start" forState: UIControlStateNormal]; btn. frame = CGRectMake (0, 0, 50, 50); btn. center = CGPointMake (x, y-80); [btn addTarget: self action: @ selector (didClicked) forControlEvents: UIControlEventTouchUpInside]; btn. backgroundColor = [UIColor blackColor]; [self. window addSubview: btn]; return YES;}-(void) animationWillStart :( NSString *) animationID context :( void *) context {NSLog (@ "% @", animationID ); NSLog (@ "% s", _ func _);}-(void) animationDidStop :( NSString *) animationID finished :( NSNumber *) finished context :( void *) context {if ([animationID isEqualToString: @ "red"]) {[UIView beginAnimations: @ "green" context: NULL]; [UIView setAnimationDelay: 5]; _ redView. bounds = CGRectMake (0, 0, 50, 50); _ greenView. bounds = CGRectMake (0, 0,100,100); _ greenView. backgroundColor = [UIColor greenColor]; _ redView. backgroundColor = [UIColor blackColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];} else if ([animationID isEqualToString: @ "green"]) {[UIView beginAnimations: @ "yellow" context: NULL]; [UIView setAnimationDelay: 5]; _ greenView. bounds = CGRectMake (0, 0, 50, 50); _ yellowView. bounds = CGRectMake (0, 0,100,100); _ greenView. backgroundColor = [UIColor blackColor]; _ yellowView. backgroundColor = [UIColor yellowColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];} else if ([animationID isEqualToString: @ "yellow"]) {[UIView beginAnimations: @ "black1" context: NULL]; [UIView setAnimationDelay: 0.9]; _ yellowView. backgroundColor = [UIColor blackColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];} else if ([animationID isEqualToString: @ "black1"]) {[UIView beginAnimations: @ "yellow1" context: NULL]; [UIView setAnimationDelay: 0.1]; _ yellowView. backgroundColor = [UIColor yellowColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];} else if ([animationID isEqualToString: @ "yellow1"]) {[UIView beginAnimations: @ "black2" context: NULL]; [UIView setAnimationDelay: 0.9]; _ yellowView. backgroundColor = [UIColor blackColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];} else if ([animationID isEqualToString: @ "black2"]) {[UIView beginAnimations: @ "yellow2" context: NULL]; [UIView setAnimationDelay: 0.1]; _ yellowView. backgroundColor = [UIColor yellowColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];} else if ([animationID isEqualToString: @ "yellow2"]) {[UIView beginAnimations: @ "black3" context: NULL]; [UIView setAnimationDelay: 0.9]; _ yellowView. backgroundColor = [UIColor blackColor]; // _ redView. backgroundColor = [UIColor redColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];} else if ([animationID isEqualToString: @ "black3"]) {[UIView beginAnimations: @ "red" context: NULL]; [UIView setAnimationDelay: 0.1]; // _ yellowView. backgroundColor = [UIColor blackColor]; _ yellowView. bounds = CGRectMake (0, 0, 50, 50); _ redView. bounds = CGRectMake (0, 0,100,100); _ redView. backgroundColor = [UIColor redColor]; [UIView setAnimationDelegate: self]; [UIView attributes: @ selector (animationWillStart: context :)]; [UIView attributes: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations] ;}}// response method-(void) didClicked {[UIView beginAnimations: @ "red" context: NULL]; // [UIView setAnimationDuration: <# (NSTimeInterval) #>] _ redView. backgroundColor = [UIColor redColor]; _ redView. bounds = CGRectMake (0, 0,100,100); // _ redView. center = CGPointMake (x, y + 100); [UIView setAnimationDelegate: self]; [UIView setAnimationWillStartSelector: @ selector (animationWillStart: context :)]; [UIView progress: @ selector (animationDidStop: finished: context :)]; [UIView commitAnimations];}-(void) applicationWillResignActive :( UIApplication *) application {// Sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationDidEnterBackground :( UIApplication *) application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits .} -(void) applicationWillEnterForeground :( UIApplication *) application {// Called as part of the transition from the background to the inactive state; here you can undo records of the changes made on entering the background .} -(void) applicationDidBecomeActive :( UIApplication *) application {// Restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} -(void) applicationWillTerminate :( UIApplication *) application {// Called when the application is about to terminate. save data if appropriate. see also applicationDidEnterBackground :.} @ end
As follows:
Basic implementation