1 前言
应用程序遇到的最简单的状态是从活动过渡到不活动,然后再返回到活动。今天我们进来用一个例子来看看其具体应用。
2 详述
这张的内容比较简单,就直接上代码了
ZYViewController.m
// // 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]; //注册通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]]; [[NSNotificationCenter defaultCenter] 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{ //隐式动画,Core Animation会将属性从其当前值流畅的过渡到我们制定的值,完成后可以执行任何操作。 [UIView animateWithDuration:0.5 animations:^{ //为标签的transform设置特定的旋转角度(以弧度为单位指定)。 label.transform = CGAffineTransformMakeRotation(M_PI); } //他们还设置一个完成程序块来调用其他方法,使文本不停反复地显示动画 completion:^(BOOL finished){ [self rotateLabelUp]; }]; } -(void)rotateLabelUp{ [UIView animateWithDuration:0.5 animations:^{ label.transform = CGAffineTransformMakeRotation(0); } completion:^(BOOL finished){ //添加判断条件 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
运行结果
控制台结果:
2013-06-08 13:20:24.265 State Lab[414:c07] application:didFinishLaunchingWithOptions:
2013-06-08 13:20:24.287 State Lab[414:c07] applicationDidBecomeActive:
2013-06-08 13:20:24.288 State Lab[414:c07] applicationDidBecomeActive:
按下Home按钮控制台结果:
2013-06-08 13:21:57.394 State Lab[414:c07] applicationWillResignActive:
2013-06-08 13:21:57.395 State Lab[414:c07] applicationWillResignActive:
2013-06-08 13:21:57.396 State Lab[414:c07] applicationDidEnterBackground:
在次运行App结果:
控制台结果
2013-06-08 13:22:44.051 State Lab[414:c07] applicationWillEnterForeground:
2013-06-08 13:22:44.052 State Lab[414:c07] applicationDidBecomeActive:
2013-06-08 13:22:44.053 State Lab[414:c07] applicationDidBecomeActive:
3 结语
以上是所有内容,希望对大家有所帮助。
Demo下载地址:http://download.csdn.net/detail/u010013695/5545277