你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(105)之處理不活動狀態

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

編輯:IOS開發綜合

1 前言
應用程序遇到的最簡單的狀態是從活動過渡到不活動,然後再返回到活動。今天我們進來用一個例子來看看其具體應用。

2 詳述
這張的內容比較簡單,就直接上代碼了


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]; 
    //注冊通知 
    [[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 

//
//  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:

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved