1 Preface
The simplest state an application encounters is transition from activity to activity, and then return to activity. Today, let's use an example to look at its specific application.
2. Details
The content here is relatively simple, so you can directly access the code.
ZYViewController. m
[Plain]
//
// ZYViewController. m
// State Lab
//
// Created by zhangyuc on 13-6-8.
// Copyright (c) 2013 zhangyuc. All rights reserved.
//
# Import "ZYViewController. h"
@ Interface ZYViewController ()
@ End
@ Implementation ZYViewController
@ Synthesize label;
@ Synthesize animate;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Registration notification
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (applicationWillResignActive :) name: UIApplicationWillResignActiveNotification object: [UIApplication sharedApplication];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (applicationDidBecomeActive :) name: UIApplicationDidBecomeActiveNotification object: [UIApplication sharedApplication];
CGRect bounds = self. view. bounds;
CGRect labelFrame = CGRectMake (bounds. origin. x, CGRectGetMidY (bounds)-50, bounds. size. width, 100 );
Self. label = [[UILabel alloc] initWithFrame: labelFrame];
Label. font = [UIFont fontWithName: @ "Helvetica" size: 70];
Label. text = @ "Archy! ";
Label. textAlignment = UITextAlignmentCenter;
Label. backgroundColor = [UIColor clearColor];
[Self. view addSubview: label];
// [Self rotatelabelDown];
}
-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) dealloc {
[Label release];
[Super dealloc];
}
-(Void) rotatelabelDown {
// For implicit Animation, Core Animation will smoothly transition the attribute from its current value to the value we have set. After that, any operations can be performed.
[UIView animateWithDuration: 0.5
Animations: ^ {
// Set the specific rotation angle (in radians) for the label's transform ).
Label. transform = CGAffineTransformMakeRotation (M_PI );
}
// They also set a completion program block to call other methods, so that the text will repeatedly display the animation
Completion: ^ (BOOL finished ){
[Self rotateLabelUp];
}];
}
-(Void) rotateLabelUp {
[UIView animateWithDuration: 0.5
Animations: ^ {
Label. transform = CGAffineTransformMakeRotation (0 );
}
Completion: ^ (BOOL finished ){
// Add judgment Conditions
If (animate)
[Self rotatelabelDown];
}];
}
-(Void) applicationWillResignActive :( UIApplication *) application
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
Animate = NO;
}
-(Void) applicationDidBecomeActive :( UIApplication *) application
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
Animate = YES;
[Self rotatelabelDown];
}
@ End
//
// ZYViewController. m
// State Lab
//
// Created by zhangyuc on 13-6-8.
// Copyright (c) 2013 zhangyuc. All rights reserved.
//
# Import "ZYViewController. h"
@ Interface ZYViewController ()
@ End
@ Implementation ZYViewController
@ Synthesize label;
@ Synthesize animate;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Registration notification
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (applicationWillResignActive :) name: UIApplicationWillResignActiveNotification object: [UIApplication sharedApplication];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (applicationDidBecomeActive :) name: UIApplicationDidBecomeActiveNotification object: [UIApplication sharedApplication];
CGRect bounds = self. view. bounds;
CGRect labelFrame = CGRectMake (bounds. origin. x, CGRectGetMidY (bounds)-50, bounds. size. width, 100 );
Self. label = [[UILabel alloc] initWithFrame: labelFrame];
Label. font = [UIFont fontWithName: @ "Helvetica" size: 70];
Label. text = @ "Archy! ";
Label. textAlignment = UITextAlignmentCenter;
Label. backgroundColor = [UIColor clearColor];
[Self. view addSubview: label];
// [Self rotatelabelDown];
}
-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) dealloc {
[Label release];
[Super dealloc];
}
-(Void) rotatelabelDown {
// For implicit Animation, Core Animation will smoothly transition the attribute from its current value to the value we have set. After that, any operations can be performed.
[UIView animateWithDuration: 0.5
Animations: ^ {
// Set the specific rotation angle (in radians) for the label's transform ).
Label. transform = CGAffineTransformMakeRotation (M_PI );
}
// They also set a completion program block to call other methods, so that the text will repeatedly display the animation
Completion: ^ (BOOL finished ){
[Self rotateLabelUp];
}];
}
-(Void) rotateLabelUp {
[UIView animateWithDuration: 0.5
Animations: ^ {
Label. transform = CGAffineTransformMakeRotation (0 );
}
Completion: ^ (BOOL finished ){
// Add judgment Conditions
If (animate)
[Self rotatelabelDown];
}];
}
-(Void) applicationWillResignActive :( UIApplication *) application
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
Animate = NO;
}
-(Void) applicationDidBecomeActive :( UIApplication *) application
{
NSLog (@ "% @", NSStringFromSelector (_ cmd ));
Animate = YES;
[Self rotatelabelDown];
}
@ End
Running result
Console result:
13:20:24. 265 State Lab [414: c07] application: didfinishlaunchingwitexceptions:
13:20:24. 287 State Lab [414: c07] applicationDidBecomeActive:
13:20:24. 288 State Lab [414: c07] applicationDidBecomeActive:
Press the Home button to view the console result:
13:21:57. 394 State Lab [414: c07] applicationWillResignActive:
13:21:57. 395 State Lab [414: c07] applicationWillResignActive:
13:21:57. 396 State Lab [414: c07] applicationDidEnterBackground:
Result of the next App running:
Console result
13:22:44. 051 State Lab [414: c07] applicationWillEnterForeground:
13:22:44. 052 State Lab [414: c07] applicationDidBecomeActive:
13:22:44. 053 State Lab [414: c07] applicationDidBecomeActive: