button的初始化方法

 //凡是继承于UIControl的控件都具有相应事件点击的能力UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

设置button的形状位置大小,字体,字体颜色,背景颜色,不同状态下button的字体和字体颜色
这里需要注意的是button设置字体和字体颜色用得时方法,而不是像labei那样用得时.text之类的

 //设置形状位置button.frame = CGRectMake(20, 100, 100, 100);//设置字体[button setTitle:@"我是按钮" forState:UIControlStateNormal];//字体颜色[button setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];//背景颜色button.backgroundColor = [UIColor lightGrayColor];//设置高亮状态时的视图[button setTitle:@"高亮状态" forState:UIControlStateHighlighted];[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

为button添加不同操作后的点击事件(以下是一个操作的事件)

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];//添加view[self.view addSubview:button];

当设置button是选择状态的时候,需要实现设置好是否选中(根据需要)
其实就是下面的一句代码

UIButton *button2  = [UIButton buttonWithType:UIButtonTypeCustom];button2.frame = CGRectMake(140, 100, 100, 100);[button2 setTitle:@"自然状态" forState:UIControlStateNormal];[button2 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];[button2 setTitle:@"选中状态" forState:UIControlStateSelected];[button2 setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];button2.backgroundColor = [UIColor lightGrayColor];//添加事件[button2 addTarget:self action:@selector(buttonSelectorAction:) forControlEvents:UIControlEventTouchUpInside];button2.selected = NO;[self.view addSubview:button2];

button的编辑状态
其实这个直到刚才还不直到有什么用,但是百度了一下就是到了,可以用在的地方,如:你必须要设置在textField完成输入后才可以点击button,不然此button是不可选的(灰色),此时的状态就是编辑编辑状态的不启用状态,代码如下:其实是下面的倒数第二句代码

UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];button3.frame = CGRectMake(260, 100, 100, 100);[button3 setTitle:@"zir状态" forState:UIControlStateNormal];[button3 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];[button3 setTitle:@"编辑状态" forState:UIControlStateDisabled];[button3 setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];button3.backgroundColor = [UIColor lightGrayColor];button3.enabled = NO;[self.view addSubview:button3];参考
//以下是我百度出来对button几种状态的解析
//设置button标题
[button1 setTitle:@"点击" forState:UIControlStateNormal];/ forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//以下是几种状态
// enum {// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
// };

为button不同状态添加图片(背景图片)
注意:
imageName是你拉入工程的图片的名字
如果是选择状态记得设置是否选中状态bButton.selected = NO;
如果是编辑状态记得设置是否启用bButton.enabled = NO;

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];aButton.frame = CGRectMake(20, 220, 100, 100);//自然[aButton setImage:[UIImage imageNamed:@"of"] forState:UIControlStateNormal];//高亮[aButton setImage:[UIImage imageNamed:@"off"] forState:UIControlStateHighlighted];[aButton addTarget:self action:@selector(aButtonAction:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:aButton];//UIButton *bButton = [UIButton buttonWithType:UIButtonTypeCustom];bButton.frame = CGRectMake(140, 220, 100, 100);[bButton setImage:[UIImage imageNamed:@"of"] forState:UIControlStateNormal];//选中[bButton setImage:[UIImage imageNamed:@"off"] forState:UIControlStateSelected];[bButton addTarget:self action:@selector(bButtonAction:) forControlEvents:UIControlEventTouchUpInside];bButton.selected = NO;[self.view addSubview:bButton];

下面这些是上面button中需要实现的点击方法

-(void)buttonAction:(UIButton *)sender
{NSLog(@"按钮响应了");}-(void)buttonSelectorAction:(UIButton *)sender{NSLog(@"按钮2响应啦");sender.selected = !sender.selected;
}-(void)aButtonAction:(UIButton *)sender{NSLog(@"图片改变了");
}
-(void)bButtonAction:(UIButton *)sender{NSLog(@"bButton响应了");sender.selected = !sender.selected;}

button的基本就到这里了
总结
1.button的初始化方法
2.button的位置大小
3.button的字体,以及颜色,背景图片,背景颜色(和label一样设置);
4.给button在不同点击状态下设置点击事件

下面用button做一个仿微信按住说话的小界面

//应用,仿微信按住说话UIButton *cButoon = [UIButton buttonWithType:UIButtonTypeCustom];cButoon.frame = CGRectMake(Screen_Width/2-40, 400, 80, 100);[cButoon setImage:[UIImage imageNamed:@"other"] forState:UIControlStateNormal];//按下时响应的事件[cButoon addTarget:self action:@selector(cButtonDown:) forControlEvents:UIControlEventTouchDown];//松开响应时间[cButoon addTarget:self action:@selector(cButtonUp:) forControlEvents:UIControlEventTouchUpInside];//以下是新内容以后会学到UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sof"]];imageView.frame = CGRectMake(Screen_Width/2+40, 400-30, 30, 30);[self.view addSubview:cButoon];[self.view addSubview:imageView];imageView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"soff"]];imageView2.frame = CGRectMake(Screen_Width/2+40, 400-30, 30, 30);imageView2.hidden = YES;//设置该图片原始是否隐藏,这里用在按下button的时候将次属性取反,图片就显示出来了[self.view addSubview:imageView2];//这个是用来在按钮按下的时候进行通话轮播的图片数组,集体使用看下面的方法实现imageView2.animationImages = @[[UIImage imageNamed:@"sof"],[UIImage imageNamed:@"soff"]];//通话轮波时间以后会说imageView2.animationDuration = 0.2;

仿微信按住说话中的方法

-(void)cButtonDown:(UIButton *)sender{NSLog(@"Doen");imageView2.hidden = NO;//开始动画[imageView2 startAnimating];}
-(void)cButtonUp:(UIButton *)sender{imageView2.hidden = YES;[imageView2 stopAnimating];NSLog(@"Up");
}

@end

IOS开发UI-------button相关推荐

  1. iOS开发UI篇—transframe属性(形变)

    iOS开发UI篇-transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...

  2. iOS开发UI基础—手写控件,frame,center和bounds属性

    iOS开发UI基础-手写控件,frame,center和bounds属性 一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4 ...

  3. android tableview实现多选功能,iOS开发UI篇-tableView在编辑状态下的批量操作(多选)...

    先看下效果图 直接上代码 #import "MyController.h" @interface MyController () { UIButton *button; } @pr ...

  4. iOS开发UI篇—多控制器和导航控制器简单介绍

    iOS开发UI篇-多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...

  5. iOS开发UI篇—UIWindow简单介绍

    iOS开发UI篇-UIWindow简单介绍 一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWi ...

  6. iOS开发UI篇—简单介绍静态单元格的使用

    iOS开发UI篇-简单介绍静态单元格的使用 一.实现效果与说明 说明:观察上面的展示效果,可以发现整个界面是由一个tableview来展示的,上面的数据都是固定的,且几乎不会改变. 要完成上面的效果, ...

  7. iOS开发UI篇—实现UITableview控件数据刷新

    iOS开发UI篇-实现UITableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  8. iOS开发UI篇—UITabBarController简单介绍

    iOS开发UI篇-UITabBarController简单介绍 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarControlle ...

  9. iOS开发UI篇—懒加载

    iOS开发UI篇-懒加载 1.懒加载基本 懒加载--也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了, ...

  10. iOS开发UI篇—UITableviewcell的性能优化和缓存机制

    iOS开发UI篇-UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...

最新文章

  1. Science:充满铵盐的环境依然发生固氮
  2. 【错误记录】Android Studio 编译时 Kotlin 代码编译报错 ( 升级支持库时处理 @NonNull 参数 )
  3. mysql 免安装重装_MYSQL的免安装的重装
  4. 汇编 编程实现从键盘输入三位以内的十进制负数_macOS上的汇编入门(二)——数学基础...
  5. 一款好用的轮播插件swiper,适用于移动端和web
  6. devc++鼠标变成了光标_Excel填充别再用鼠标拖拉了!用这4个方法,效率至少高10倍!...
  7. CF Gym102059 H. Fractions
  8. Web开发入门疑问收集(不定期更新)
  9. sql server中的varchar和Nvarchar有什么区别?
  10. 斐波那契数列,递归与非递归c语言实现
  11. git 上传项目到linux仓库_总结:上传python项目至git上前的一些准备工作
  12. mysql error 1130 hy000:Host ‘localhost‘ is not allowed to connect to this mysql server 解决方案
  13. MATLAB rolcus函数,自动控制原理实验报告 .doc
  14. java 比较器类_高级编程之(Java常用类(Java比较器))
  15. Docker 删除所有容器和镜像,从头来过!
  16. 华为手机android怎么解锁,华为手机解锁密码忘了怎么办?华为手机找回锁屏密码方法...
  17. 端口映射抖音去水印工具网页源码
  18. Impala sql实现同比计算(lag函数)
  19. 【STC单片机学习】第九课:单片机按键使用
  20. 用Python写个空课表生成器-Excel文件操作实例

热门文章

  1. 一个程序员的自白:我为什么写博客
  2. 数据结构课程设计(八)---家谱管理系统(十几个功能)
  3. 【Linux】入门介绍
  4. 全球及中国物流中心产业运营价值与投资可行性研究报告2022版
  5. CELLID GET
  6. 胖子和瘦子谁更怕冷?
  7. python 爬取简单静态网站之电影天堂
  8. 数据库的概念模型与关系模型的设计与实现
  9. 曼哈顿距离(值得收藏)
  10. Cascade R-CNN解析