标签:
1 #import "ViewController.h" 2 3 @interface ViewController () 4 { 5 UILabel *lable; 6 BOOL moveFlag; 7 NSInteger length; 8 UIButton *btn; 9 NSTimer *timer; 10 } 11 12 @end 13 14 @implementation ViewController 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 moveFlag = NO; 19 length = 10; 20 21 self.view.backgroundColor = [UIColor darkGrayColor]; 22 lable = [[UILabel alloc] init]; 23 lable.frame = CGRectMake(50, 100, 280, 50); 24 lable.text = @"点我啊"; 25 lable.backgroundColor = [UIColor whiteColor]; 26 [self.view addSubview:lable]; 27 28 // 按钮UIButton 29 btn = [UIButton buttonWithType:UIButtonTypeCustom]; 30 // 设置按钮坐标、大小 31 btn.frame = CGRectMake(170, 250, 50, 50); 32 // 设置背景图 33 [btn setImage:[UIImage imageNamed:@"fire_down.png"] forState:UIControlStateNormal]; 34 [btn setImage:[UIImage imageNamed:@"blue_down.png"] forState:UIControlStateHighlighted]; 35 36 37 // 设置标题(状态标题) 38 // [btn setTitle:@"开始" forState:UIControlStateNormal]; 39 // [btn setTitle:@"按下" forState:UIControlStateHighlighted]; 40 41 // 给按钮添加 [关联事件] 42 [btn addTarget:self action:@selector(touchMe) forControlEvents:UIControlEventTouchUpInside]; 43 // 添加背景色 44 [btn setBackgroundColor:[UIColor cyanColor]]; 45 46 // 按钮变圆 47 btn.layer.cornerRadius = 20; 48 // 添加按钮到视图上 49 [self.view addSubview:btn]; 50 51 } 52 53 -(void) touchMe 54 { 55 //判断完了小球动没有动 56 if (moveFlag ==NO) { 57 moveFlag = YES; 58 } 59 else 60 moveFlag = NO; 61 62 //当小球没有动得时候,点小球-变动 63 if (moveFlag) { 64 timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(bollMove) userInfo:nil repeats:YES]; 65 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; 66 } 67 else 68 [timer invalidate]; 69 //当小球动得时候,点小球-不动 70 71 } 72 73 -(void) bollMove 74 { 75 76 if (btn.frame.origin.y>600) { 77 length = -10; 78 79 } 80 else if (btn.frame.origin.y<0) 81 { 82 length = 10; 83 } 84 85 CGRect rect = btn.frame; 86 rect.origin.y += length; 87 btn.frame = rect; 88 89 }
标签:
原文地址:http://www.cnblogs.com/oc-bowen/p/5076721.html