UI01_UIButton
来源:互联网 发布:软件岗位分类 编辑:程序博客网 时间:2024/05/06 16:10
//
// ViewController.m
// UI01_UIButton
//
// Created by lanou on 16/1/14.
// Copyright © 2016年 lanou. All rights reserved.
//
import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];//初始化一个按钮
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//指定按钮的大小
_button.frame = CGRectMake(100, 100, 150, 60);
//设置颜色
_button.backgroundColor = [UIColor redColor];
//设置文字
[_button setTitle:@”1” forState:UIControlStateNormal];
//点击高亮
_button.showsTouchWhenHighlighted = YES;
//添加到controller上
[self.view addSubview:_button];
//给按钮添加点击事件
[_button addTarget:self action:@selector(youTouchMe) forControlEvents:UIControlEventTouchUpInside];
//改控制器的背景色
self.view.backgroundColor = [UIColor grayColor];//定时器
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeIsOver) userInfo:nil repeats:NO];//定时器控制0.1秒+1
_time = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(upUp) userInfo:nil repeats:YES];// Do any additional setup after loading the view.
}
-(void)upUp
{
_flag++;
[_button setTitle:[NSString stringWithFormat:@”%ld”,_flag] forState:UIControlStateNormal];
}
-(void)timeIsOver
{
//R G B
//随机一个0-255的数字
int red = arc4random()%256;
int green = arc4random()%256;
int blue = arc4random()%256;
//根据RGB赋颜色
self.view.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
}
//点击按钮之后就走这个方法
-(void)youTouchMe
{
//R G B
//随机一个0-255的数字
int red = arc4random()%256;
int green = arc4random()%256;
int blue = arc4random()%256;
//根据RGB赋颜色
self.view.backgroundColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
//停止定时器
[_time invalidate];
_time = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end