1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #import "myButton.h"
@implementation myButton
+(UIButton *)buttonWithFrame:(CGRect)frame BGColor:(UIColor *)color Title:(NSString *)title NormalImage:(UIImage *)normalImage Tag:(int)tag Method:(SEL)method Object:(id)object{ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = frame; button.backgroundColor = color; [button setTitle:title forState:UIControlStateNormal]; button.tag = tag; [button setBackgroundImage:normalImage forState:UIControlStateNormal]; [button addTarget:object action:method forControlEvents:UIControlEventTouchUpInside]; return button; }
@end
#import "ViewController.h" #import "myButton.h" @interface ViewController () { UIActivityIndicatorView *act; } @end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad];
[self createBtn]; [self createActivityIndicatorView]; } -(void)onClick:(UIButton*)button{ if (act.isAnimating == YES) { [act stopAnimating]; }else if(act.isAnimating == NO){ [act startAnimating]; } } -(void)createActivityIndicatorView{
act = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; act.frame = CGRectMake(100, 300, 50, 50);
act.center = self.view.center; [self.view addSubview:act]; act.backgroundColor = [UIColor redColor];
act.transform = CGAffineTransformMakeScale(4, 4); act.tag = 10;
act.hidesWhenStopped = YES; } -(void)createBtn{ UIButton *startBtn = [myButton buttonWithFrame:CGRectMake(100, 100, 50, 50) BGColor:[UIColor lightGrayColor] Title:@"开始" NormalImage:nil Tag:10 Method:@selector(onClick:) Object:self]; [self.view addSubview:startBtn]; UIButton *endBtn = [myButton buttonWithFrame:CGRectMake(180, 100, 50, 50) BGColor:[UIColor lightGrayColor] Title:@"停止" NormalImage:nil Tag:11 Method:@selector(onClick:) Object:self]; [self.view addSubview:endBtn]; [self.view setBackgroundColor:[UIColor yellowColor]]; } @end
|