您的位置:首页 > 产品设计 > UI/UE

UI控件笔记(三):UI之UIButton的属性

2016-05-17 10:05 471 查看
一、UIButton的相关操作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

self.window = [[UIWindow
alloc] initWithFrame:[UIScreen
mainScreen].bounds];

self.window.backgroundColor = [UIColor
whiteColor];

1、UIButton的创建过程

1.1、用类方法实例化一个UIButton对象,参数可以选择不同的button类型,现在我们选择的是圆角按钮,用来添加文字

UIButton *firstBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];

1.2、设置button的frame

firstBtn.frame =
CGRectMake(0,
20, 320, 44);

1.3、添加到父视图

[self.window
addSubview:firstBtn];

注意:是否需要对firstBtn执行release?不需要release,因为是用类方法创建的

1.4、设置圆角按钮的标题文字

[firstBtn setTitle:@"pp"
forState:UIControlStateNormal];

2、UIButton的按钮类型

A、roundRect,类型:圆角,用途:写字

B、add,类型:加号,用途:和添加东西相关,看美工

UIButton *secondBtn = [UIButton
buttonWithType:5];

secondBtn.frame =
CGRectMake(0,
64, 320, 44);

[self.window
addSubview:secondBtn];

C、info,类型:i号,用途:和提示相关,看美工

UIButton *thirdBtn = [UIButton
buttonWithType:3];

thirdBtn.frame =
CGRectMake(0,
108, 320, 44);

[self.window
addSubview:thirdBtn];

注意:ios7之后,infoDark、infoLight、DetailDisclosure效果都一样了

D、custom,类型:custom,用途:贴图,让美工切

UIButton *fourthBtn = [UIButton
buttonWithType:UIButtonTypeCustom];

fourthBtn.frame =
CGRectMake(0,
152, 121, 96);

[self.window
addSubview:fourthBtn];

[fourthBtn setImage:[UIImage
imageNamed:@"DOVE 1.png"]
forState:UIControlStateNormal];

//在使用custom的按钮时,一般要保证按钮的宽和高应该正好等于图片的宽和高

3、循环做按钮

在一行中,循环做4个按钮,每个按钮的大小都是80*80,依次为文字1、贴图0、+号5、i号2-4的按钮

NSArray *typeArr =
@[@"1",@"0",@"5",@"2"];//按钮类型其实是一个枚举,都是整数

for(int i =
0;i<4;i++)

{

UIButton *btn = [UIButton
buttonWithType:[typeArr[i]
intValue]];//把数组中对应下标的字符串转为整数,当做这里的枚举值

btn.frame =
CGRectMake(i*80,
250, 80, 80);

[self.window
addSubview:btn];

if(i == 0)

{

[btn setTitle:@"1"
forState:UIControlStateNormal];

}

if(i == 1)

{

[btn setImage:[UIImage
imageNamed:@"DOVE 1.png"]
forState:UIControlStateNormal];

}

}

UIButton *nameBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];

nameBtn.frame =
CGRectMake(0,
330, 60, 30);

[nameBtn setTitle:@"lily"
forState:UIControlStateNormal];//普通状态

[nameBtn setTitleColor:[UIColor
redColor] forState:UIControlStateNormal];//普通红字

[nameBtn setTitle:@"lucy"
forState:UIControlStateHighlighted];//高亮状态,from普通状态按下去时就是高亮

[nameBtn setTitleColor:[UIColor
greenColor] forState:UIControlStateSelected];//选中绿字

[nameBtn setTitle:@"pp"
forState:UIControlStateSelected];//选中状态

nameBtn.selected =
NO;//按钮的选中属性,yes为选中,no为普通

nameBtn.tag =
1000;

[self.window
addSubview:nameBtn];

4、按钮事件的添加

IOS中触发一个button的事件方法一般是使用,目标-方法机制,就是当一个事件发生时,让哪个目标对象,执行什么方法

[nameBtn addTarget:self
action:@selector(nameBtnDown)
forControlEvents:UIControlEventTouchUpInside];

//这个按钮的第三个参数所代表的事件,会使得第一个参数的对象去调用第二个参数的方法

4.1、按钮的常用事件及其使用环境

A、按下,touchDown,长按弹出菜单

UIControlEventTouchDown

B、按下并在按钮内部松手,touchUpInside,普通点击

UIControlEventTouchUpInside

C、按下并在按钮内部拖动,touchDragInside,拖动一个按钮移动

UIControlEventTouchDragInside

注意:B、C别写错了

//三个按钮改label

UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(0,
20, 320,
44)];

[self.window
addSubview:label];

[label release];

//一会儿按钮点击的时候要改变这个label的文字,就得找到这个label,然后改变这个label的text属性,就得能找到这个label,怎么找?通过tag值来找

label.tag = 2000;

for(int i =
0;i<3;i++)

{

UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];

btn.frame =
CGRectMake(i*100,
64, 100, 30);

[btn setTitle:[NSString
stringWithFormat:@"%d",i]
forState:UIControlStateNormal];

[self.window
addSubview:btn];

//btn的方法

[btn addTarget:self
action:@selector(btnDown:)
forControlEvents:UIControlEventTouchUpInside];

//这个按钮的方法带了一个UIButton类型的参数,所以哪个按钮触发了方法,参数中的btn就是哪个按钮

}

[self.window
makeKeyAndVisible];

return
YES;

}

4.2、这是一个有参数的按钮方法,参数类型不用想,只能是UIButton*,什么的方法,就是什么的参数

-(void)btnDown:(UIButton*)btn//这个参数就是被点的按钮,就等于把被点的按钮对象传过来了

{

NSLog(@"%@",btn.titleLabel.text);

//改变label的文字

//找到label

UILabel *lab = (UILabel*)[self.window
viewWithTag:2000];

//改字

lab.text = btn.titleLabel.text;

}

4.3下面这个方法是nameBtn的点击事件,当nameBtn有一些事件的时候,会调用这个方法

-(void)nameBtnDown

{

UIButton *tempBtn = (UIButton*)[self.window
viewWithTag:1000];//通过1000这个值找到他对应的按钮

tempBtn.selected = !tempBtn.selected;//把bool值置反,从而实现改变按钮状态的功能

//通过改变按钮的选中或者普通状态改变按钮的文字

NSLog(@"别乱点");

//获取按钮的文字

NSLog(@"%@",tempBtn.titleLabel.text);

}

二、UIButton小结

1、实例化方法,类方法[UIButton buttonWithType:类型]

2、类型:roundRect、custom、Add、info(三种)四种

圆角写字 透明贴图 +号 i号

3、设置按钮的矩形:btn.frame

4、圆角按钮设置title:btn setTitle:title字符串 state:状态

5、按钮的状态:normal、highLighted、selected

普通 按下高亮 选中

6、改变按钮状态:btn.selected,YES或NO

7、按钮的事件:首先要有一个方法给他用
btn addTarget:目标对象 action:@selector(方法名) event:事件

8、按钮的事件:touchDown、touchUpInside、touchDragInside

按下 按下在内部松手 按下在内部拖动

9、如何找到btn上的文字:btn.titleLabel.text
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: