欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发控件视图day02:动态创建按钮、按钮状态、按钮动画

程序员文章站 2023-01-21 21:23:22
IOS开发控件视图day02...

1、动态创建按钮
(1)创建按钮(UIButton)

UIButton *button1 = [[UIButton alloc]init];

(2)设置按钮的frame

button1.frame = CGRectMake(20,80,120,80);

(3)设置按钮上显示的文字
设置默认状态下显示的文字

[button1 setTitle:@“阚志华” forState:UIControlStateNormal];

设置高亮状态下的显示文字

[button1 setTitle:@“李小伟” forState:UIControlStateHighlighted];

(4)设置不同状态下显示的文字颜色
设置默认状态下的文字颜色

[button1 setTitelColor:[UIColor redColor] forState:UIControlStateHighlighted];

设置高亮状态下的文字颜色

[button1 setTitelColor:[UIColor blueColor] forState:UIControlStateHighlighted];

(5)设置按钮上的背景图片

UIImage *imgNormal = [UIImage imageNamed:@"img01"];
UIImage *imgHighLighted = [UIImage imageNamed:@"img01"];

设置默认状态下的背景图片

[button1 setBackgrundImage:imgNormal forState:UIControlStateNormal];

设置高亮状态下的背景图片

[button1 setBackgrundImage:imgHighLighted forState:UIControlStateHighlighted];

(6)为按钮注册单击事件

[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

(7)将动态创建的按钮加到控制器所管理的那个view中

[self.view addSubview:button1];

(8)实现单击实现函数

-(void)buttonClick
{
	//实现函数
}
frame:能修改位置和尺寸(左上角坐标)
center:能修改位置(中心点坐标)
bounds:能修改尺寸

2、实现简单平移动画
(1)头尾式

[UIView beginAnimations:nil context:nil];//开启动画
[UIView setAnimationDuration:1];//设置动画执行时间
/**需要执行动画的代码**/
[UIView commitAnimations];//提交动画

(2)Block式

[UIView animateWithDuration:0.5 animations:^]
{
	/**需要执行动画的代码**/
}]

本文地址:https://blog.csdn.net/wenyu_Saitama/article/details/107157543