UI基础控件UIButton
一:UI基础 Button控件
1,简单说明:UIView和UIViewController间的关系
一个应用并不一定要有UIViewController,但是为了管理界面事件(比如按钮点击事件)一般我们在创建应用时要先创建控制器,控制器拥有一个UIView属性。
UIView用来展示数据、及用户输入数据、监听事件的触发(比如按钮的touchupinside事件)
而控制可以用来处理这些事件,赋值UIView的管理。
按钮可以展示文字和图片,也就是说按钮具有这些属性。
2,按钮的三种状态
normal (默认状态) UIControlStateNormal
highlighted(高亮状态)UIControlStateHighted
disabled (BOOL类型) UIControlStateDisabled 失去用户交互能力
#import "ViewController.h" #define HEADBTNX 110 #define HEADBTNY 60 #define HEADBTNWH 100 typedef enum { KMOVETOP = 10, KMOVEBOTTOM, KMOVELEFT, KMOVERIGHT }LLKOMVE; #define KMOVEDEALT 50 @interface ViewController () @property (nonatomic, weak) UIButton *headBtn; // 头部按钮 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self loadBtnView]; // UITextField *textFied = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 40, 20)]; // textFied.backgroundColor = [UIColor purpleColor]; // [self.view addSubview:textFied]; // [textFied resignFirstResponder]; } #pragma mark - 加载界面按钮视图控件 - (void)loadBtnView { //--------------------1,创建头像按钮 UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom]; headBtn.frame = CGRectMake(HEADBTNX, HEADBTNY, HEADBTNWH, HEADBTNWH); // 普通状态 [headBtn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal]; [headBtn setTitle:@"点我啊" forState:UIControlStateNormal]; [headBtn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; // 高亮状态 [headBtn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted]; [headBtn setTitle:@"摸我啊" forState:UIControlStateHighlighted]; [headBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; self.headBtn = headBtn; [self.view addSubview:headBtn]; // -------------------2,创建向上按钮 UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeCustom]; topBtn.frame = CGRectMake(60, 350, 30, 30); [topBtn setBackgroundImage:[UIImage imageNamed:@"top_normal"] forState:UIControlStateNormal]; [topBtn setBackgroundImage:[UIImage imageNamed:@"top_highlighted"] forState:UIControlStateHighlighted]; topBtn.tag = 10; // 添加按钮事件: [topBtn addTarget:self action:@selector(moveBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:topBtn]; // -------------------3,创建向下按钮 UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom]; bottomBtn.frame = CGRectMake(60, 400, 30, 30); [bottomBtn setBackgroundImage:[UIImage imageNamed:@"bottom_normal"] forState:UIControlStateNormal]; [bottomBtn setBackgroundImage:[UIImage imageNamed:@"bottom_highlighted"] forState:UIControlStateHighlighted]; bottomBtn.tag = 11; [bottomBtn addTarget:self action:@selector(moveBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:bottomBtn]; // -------------------4,创建向left按钮 UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; leftBtn.frame = CGRectMake(20, 375, 30, 30); [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal]; [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted]; leftBtn.tag = 12; [leftBtn addTarget:self action:@selector(moveBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:leftBtn]; // -------------------4,创建向right按钮 UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; rightBtn.frame = CGRectMake(100, 375, 30, 30); [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal]; [rightBtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted]; rightBtn.tag = 13; [rightBtn addTarget:self action:@selector(moveBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:rightBtn]; // -------------------5,创建plus到按钮 UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom]; plusBtn.frame = CGRectMake(180, 375, 30, 30); [plusBtn setBackgroundImage:[UIImage imageNamed:@"plus_normal"] forState:UIControlStateNormal]; [plusBtn setBackgroundImage:[UIImage imageNamed:@"plus_highlighted"] forState:UIControlStateHighlighted]; plusBtn.tag = 50; [plusBtn addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:plusBtn]; // -------------------6,创建minus到按钮 UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom]; minusBtn.frame = CGRectMake(220, 375, 30, 30); [minusBtn setBackgroundImage:[UIImage imageNamed:@"minus_normal"] forState:UIControlStateNormal]; [minusBtn setBackgroundImage:[UIImage imageNamed:@"minus_highlighted"] forState:UIControlStateHighlighted]; minusBtn.tag = 51; [minusBtn addTarget:self action:@selector(scale:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:minusBtn]; } #pragma mark - 按钮缩小放大事件 - (void)scale:(UIButton *)btn { CGRect tempRect = self.headBtn.bounds; if (btn.tag == 50) { tempRect.size.width += 20; tempRect.size.height += 20; }else { tempRect.size.width -= 20; tempRect.size.height -= 20; } [UIView animateWithDuration:1.0 animations:^{ self.headBtn.bounds = tempRect; }]; } #pragma mark - 按钮移动事件 - (void)moveBtn:(UIButton *)btn { CGPoint tempPoint = self.headBtn.center; switch (btn.tag) { case KMOVETOP: tempPoint.y -= KMOVEDEALT; break; case KMOVEBOTTOM: tempPoint.y += KMOVEDEALT; break; case KMOVELEFT: tempPoint.x -= KMOVEDEALT; break; case KMOVERIGHT: tempPoint.x += KMOVEDEALT; break; } [UIView animateWithDuration:1.0 animations:^{ self.headBtn.center = tempPoint; }]; } @end
补充:
1>退出键盘的两种方式
resignFirstResponder
当叫出键盘的那个控件(第一响应者)调用这个方法时,就能退出键盘
self.view endEditing
只要调用这个方法的控件内部存在第一响应者,就能退出键盘
2> 修改对象的结构体成员
在OC中,不允许直接修改“对象”的“结构体属性”的“成员”,但是允许修改“对象”的“结构体属性”
修改结构体属性的成员方法如下:
(1)使用临时变量记录对象的结构体属性
(2) 修改临时变量的属性
(3)将临时变量重新设置给对象的结构体属性
3> frame & bounds & center
(1) frame可以修改对象的位置和尺寸
(2)bounds可以修改对象的尺寸 (以自己的左上角为0,0点)
(3) center可以修改对象的位置( 只有x,y属性值)