iOS - 等待UIActivityIndicatorView

头文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    //定义等待控件UIActivityIndicatorView
    UIActivityIndicatorView* _aiv;
}

//定义属性UIActivityIndicatorView
@property(retain,nonatomic)UIActivityIndicatorView* aiv;

@end

源文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize aiv = _aiv;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self initAiv];
    [self initBtns];
}

-(void) initBtns{
    UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(100, 200, 100, 50);
    btn1.backgroundColor = [UIColor greenColor];
    [btn1 setTitle:@"开始" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btn1click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
}

bool isStart = false;
-(void) btn1click:(UIButton*) btn{
    isStart = !isStart;
    if(isStart){
        [_aiv startAnimating];
        [btn setTitle:@"停止" forState:UIControlStateNormal];
    }
    else{
        [_aiv stopAnimating];
        [btn setTitle:@"开始" forState:UIControlStateNormal];
    }
}

-(void) initAiv{
    //初始化UIActivityIndivatorView
    _aiv = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _aiv.frame = CGRectMake(100, 100, 100, 100);
    _aiv.color = [UIColor orangeColor];
    _aiv.hidesWhenStopped = NO;
    
    [self.view addSubview:_aiv];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容