IOS開發(105)之處理不活動狀態

來源:互聯網
上載者:User
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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.