iOS之加载启动图过程

#import "CXDLaunchingViewController.h"
#import "Masonry.h" //自动适配类
#import "HttpTool.h" //网络请求工具类
#import "HttpRequestMacro.h" //网络请求参数
#import "AllControllerDispatchTool.h" //调度类
#import "GoodProductsViewController.h"//主页面

#define LAUNCHING_IMAGEVIEW_NAME @"launchingName"

@interface CXDLaunchingViewController ()
//背景
@property (strong, nonatomic) UIImageView *launchingBackGroundImageView;
//logo
@property (strong, nonatomic) UIImageView *markImageView;
//动画关键定时器(本次使用动画加载,没有使用定时器)
@property (strong, nonatomic) NSTimer *launchingTimer;

@end

@implementation CXDLaunchingViewController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    //加载背景(默认系统图)
    [self.view addSubview:self.launchingBackGroundImageView];
    [self.view addSubview:self.markImageView];
    
    //添加约束(自动布局 Masony)
    //为了避免使用block的时候出现循环引用,使用__weak
    __weak typeof(self) vc = self;
    [_markImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       //约束相对宽高固定
        CGFloat width = vc.view.bounds.size.width * 0.5;
        CGFloat height = width * 0.4;
        make.size.mas_equalTo(CGSizeMake(width, height));

       //约束相对坐标固定
        CGFloat viewHeight = vc.view.bounds.size.height;
        make.centerX.mas_equalTo(vc.view.mas_centerX);
        make.centerY.mas_equalTo(vc.view.mas_centerY).offset(-viewHeight/4);
        
        //先加载本地保存的【上一次请求来的数据】
        [self loadLaunchingImageView];
        
        //向服务器发起请求,获取最新的launching启动图,并保存到本地;方便下次运行时使用
        [self getNewImageView];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    //让背景动起来;动画效果
        
    [UIView animateWithDuration:4.0 animations:^{
        CGRect rect = _launchingBackGroundImageView.frame;
        rect.origin = CGPointMake(-100, -100);
        rect.size = CGSizeMake(rect.size.width+200, rect.size.height+200);
        _launchingBackGroundImageView.frame = rect;
    } completion:^(BOOL finished) {
        //动画结束之后直接进入第一个界面
        [AllControllerDispatchTool createViewControllerWithIndex:0];
    }];
}

#pragma mark - 加载方法
- (void)loadLaunchingImageView
{
    NSData *imageData = [[NSUserDefaults standardUserDefaults] objectForKey:LAUNCHING_IMAGEVIEW_NAME];
   //判空操作
    if (imageData) {
        self.launchingBackGroundImageView.image = [UIImage imageWithData:imageData];
    }
}

- (void)getNewImageView
{
    [HttpTool postWithPath:HTTP_LAUNCH_SCREEN
                    params:@{
                             @"client":@2
                            }
                   success:^(id JSON) {
    //网络请求成功后的block回调
                       //NSLog(@"%@",JSON);
                       NSString *imageUrl = [[JSON objectForKey:@"data"] objectForKey:@"picurl"];
                       //通过图片url获取图片data对象
                       NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
                       //把data对象本地保存到plist文件当中
                       [[NSUserDefaults standardUserDefaults] setObject:imageData forKey:LAUNCHING_IMAGEVIEW_NAME];
    }
                   failure:^(NSError *error) {
    //网络请求失败后的block回调
                       NSLog(@"%@",error);
                   }];
}


#pragma mark - 懒加载
//背景
-(UIImageView *)launchingBackGroundImageView
{
    if (!_launchingBackGroundImageView) {
        _launchingBackGroundImageView = [[UIImageView alloc] init];
        _launchingBackGroundImageView .frame = self.view.bounds;
        _launchingBackGroundImageView.backgroundColor = [UIColor darkGrayColor];
        _launchingBackGroundImageView.image = [UIImage imageNamed:@"defaultCover"];
    }
    return _launchingBackGroundImageView;
}

//logo
- (UIImageView *)markImageView
{
    if (!_markImageView) {
        _markImageView = [[UIImageView alloc] init];
        _markImageView.backgroundColor = [UIColor clearColor];
        _markImageView.image = [UIImage imageNamed:@"ic_splash_logo"];
    }
    return _markImageView;
}

PS:用户第一次进入程序,会动态加载本地图片,同时发起网络请求获取最新图片数据,保存到本地,下次启动自动加载最新图片数据

PSS:规定时间后,进入主界面

PSSS:以上是实现过程的部分代码,仅供参考

转载于:https://www.cnblogs.com/chixuedong/p/5239995.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值