头文件
#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
网友评论