iOS开发中在加载页面添加菊花动画(非第三方)

时间:2022-01-08 13:44:35

环境:系统版本:OSX 10.10.2Xcodel版本:6.3.2 模拟器:iPhone6   其他:使用MRC

功能:在加载页面添加菊花动画

1.新建一个类MyActivityIndicatorView,继承于UIActivityIndicatorView,添加初始化方法

先定义宏(模拟器为iPhone6)

#define kWidth 375
#define KHeight 667
#define MYCOLOR [UIColor blackColor]

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 菊花背景的大小
        self.frame = CGRectMake(kWidth/2-50, KHeight/2-50, 100, 100);
        // 菊花的背景色
        self.backgroundColor = MYCOLOR;
        self.layer.cornerRadius = 10;
        // 菊花的颜色和格式(白色、白色大、灰色)
        self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        // 在菊花下面添加文字
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 80, 40)];
        label.text = @"loading...";
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        [self addSubview:label];
    }
    return  self;
}


2.在加载网络之前调用该方法

// 自带菊花方法
    self.myActivityIndicatorView = [[MyActivityIndicatorView alloc]init];
    [self.view addSubview:_myActivityIndicatorView];
    // 动画开始
    [_myActivityIndicatorView startAnimating];

3.网络请求完成,数据加载后调用取消动画方法

// 动画结束
    [_myActivityIndicatorView stopAnimating];