欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios 自定义加载动画效果

程序员文章站 2024-01-14 09:15:58
在开发过程中,可能会遇到各种不同的场景需要等待加载成功后才能显示数据。以下是自定义的一个动画加载view效果。 在UIViewController的中加载等到效果,如下 BIDActivityNote.m 代码实现效果: ......

在开发过程中,可能会遇到各种不同的场景需要等待加载成功后才能显示数据。以下是自定义的一个动画加载view效果。

ios 自定义加载动画效果    ios 自定义加载动画效果

在UIViewController的中加载等到效果,如下

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //将view背景颜色变更为黄色
    self.view.backgroundColor = [UIColor yellowColor];
    
    //在self.view上加载提示框
    [[BIDActivityNote sharedInstance] AddActivityView:self.view];
    
    //延时3分钟后移除提示框
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[BIDActivityNote sharedInstance] RemoveActivityView];
    });
}
BIDActivityNote.h 实现代码
//
//  BIDActivityNote.h
//  MobileShop
//
//  Created by eJiupi on 15-7-23.
//  Copyright (c) 2014年 xujinzhong. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BIDActivityNote : NSObject

+ (BIDActivityNote*)sharedInstance;

- (void)AddActivityView:(UIView*)subView;

- (void)RemoveActivityView;

@end

BIDActivityNote.m 代码实现效果:

//
//  BIDActivityNote.m
//  MobileShop
//
//  Created by eJiupi on 15-7-23.
//  Copyright (c) 2014年 xujinzhong. All rights reserved.
//

#import "BIDActivityNote.h"

@interface BIDActivityNote ()

@property (strong, nonatomic) UIView *subView;
@property (strong, nonatomic) UIActivityIndicatorView *act;

@end

@implementation BIDActivityNote

+ (BIDActivityNote*)sharedInstance
{
    static BIDActivityNote* instance = nil;
    if (instance == nil)
    {
        instance = [[BIDActivityNote alloc] init];
    }
    return instance;
}

- (id)init
{
    self = [super init];
    if (self)
    {
        NSInteger w = [UIScreen mainScreen].bounds.size.width;
        NSInteger h = [UIScreen mainScreen].bounds.size.height;
        self.subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
        self.subView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];

        self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        //只能设置中心,不能设置大小
        self.act.center = CGPointMake(w/2.f, h/2.f);
        //设置活动指示器的颜色
        self.act.color=[UIColor whiteColor];
        
        [self.act startAnimating]; // 开始旋转
        [self.act stopAnimating]; // 结束旋转
        //[self.act setHidesWhenStopped:YES]; //当旋转结束时隐藏
        
        [self.subView addSubview:self.act];
    }
    return self;
}

- (void)AddActivityView:(UIView*)subView
{
    //启动
    [self.act startAnimating];
    [subView addSubview:self.subView];
    
    //实现动画效果
    self.subView.transform = CGAffineTransformScale(self.subView.transform, 0, 0);
    [UIView animateWithDuration:2 animations:^{
        self.subView.transform = CGAffineTransformIdentity;
    }]; 
}

- (void)RemoveActivityView
{
    [UIView animateWithDuration:0.7 animations:^{
        self.subView.transform = CGAffineTransformScale(self.subView.transform, 0, 0);
    } completion:^(BOOL bfinished){
        if (bfinished) {
            //停止
            [self.act stopAnimating];
            [self.subView removeFromSuperview];
        }
    }];
}

@end