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