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

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值