当前位置:编程学习 > wap >>

IOS开发(105)之处理不活动状态

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" 
 
@inte易做图ce 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"

@inte易做图ce 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];
//&n

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,