按钮的基本使用
一:学习按钮需要掌握的知识点
1:使用代码创建、添加控件
2:分别通过 storyboard 和 代码 设置按钮在不同状态的背景、文字、文字颜色
3:分别通过 storyboard 和 代码 监听按钮点击
4:修改控件的位置和尺寸
5:制作简单的动画效果
6:掌握frame、center、bounds、transform的使用
1:normal(普通状态), 默认情况(Default),对应的枚举常量:UIControlStateNormal
2:highlighted(高亮状态),按钮被按下去的时候(手指还未松开),对应的枚举常量:UIControlStateHighlighted
3:disabled(失效状态,不可用状态),如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击,对应的枚举常量:UIControlStateDisable
三:按钮位置的修改
通过修改控件的frame属性就可以修改控件在屏幕上的位置和尺寸
比如点击“向上”按钮,让按钮的y值减小即可
- (IBAction)top:(UIButton *)sender {
CGRect btnFrame = self.headBtn.frame;
btnFrame.origin.y -= 10;
self.headBtn.frame = btnFrame;
}
下面代码是错误的,OC语法规定:不允许直接修改对象的结构体属性的成员
self.headBtn.frame.origin.y -= 10;
四:代码创建按钮
- (void)viewDidLoad {
[super viewDidLoad];
//1:创建一个button
UIButton *button = [[UIButton alloc]init];
//2:加到view中
[self.view addSubview:button];
//3:设置frame
button.frame = CGRectMake(50, 50, 100, 100);
//4:设置背景颜色
//button.backgroundColor = [UIColor blueColor];
//通过文件名加载图片,凡是png图片,都不加拓展名
UIImage *image1 = [UIImage imageNamed:@"btn_01"];
UIImage *image2 = [UIImage imageNamed:@"btn_02"];
//正常模式下图片
[button setBackgroundImage:image1 forState:UIControlStateNormal];
//高亮状态下的图片
[button setBackgroundImage:image2 forState:UIControlStateHighlighted];
//5:设置字体
//字体内容
[button setTitle:@"点我" forState:UIControlStateNormal];
[button setTitle:@"小样儿" forState:UIControlStateHighlighted];
//字体颜色
[button setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
//7:再添加一个按钮
UIButton *btn= [UIButton buttonWithType:UIButtonTypeContactAdd];
[self.view addSubview:btn];
//设置间距
btn.center = CGPointMake(200, 200);
//8:加监听
[btn addTarget:self action:@selector(onclick1:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(onclick1:) forControlEvents:UIControlEventTouchUpInside];
}
五:实现简单动画 1:首尾式
[UIView beginAnimations:nil context:nil];
/** 需要执行动画的代码 **/
[UIView commitAnimations];
2:block式
[UIView animateWithDuration:0.5 animations:^{
/** 需要执行动画的代码 **/
}];
六:修改控制的尺寸和位置
通过以下属性可以修改控件的位置
frame.origin
center
通过以下属性可以修改控件的尺寸
frame.size
bounds.size
七:按钮的样式
在用代码创建按钮的同时指定按钮样式
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
storyboard中按钮的各种样式可自己去试验
八:连线总结
1.IBAction:
①: 能保证方法可以连线
②: 相当于void
2.IBOutlet:
能保证属性可以连线
3.常见错误
setValue:forUndefinedKey:]: this class is not key value coding
错误原因是:连线出问题了
九:按钮的长见设置
- (NSString *)titleForState:(UIControlState)state;
获得按钮的文字
- (UIColor *)titleColorForState:(UIControlState)state;
获得按钮的文字颜色
- (UIImage *)imageForState:(UIControlState)state;
获得按钮内部的小图片
- (UIImage *)backgroundImageForState:(UIControlState)state;
获得按钮的背景图片
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
设置按钮的文字
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
设置按钮的文字颜色
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
设置按钮内部的小图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
设置按钮的背景图片
设置按钮的文字字体(需要拿到按钮内部的label来设置)
btn.titleLabel.font = [UIFont systemFontOfSize:13];