UI Button
iOS开发UI篇—Button基础
一、简单说明
一般情况下,点击某个控件后,会做出相应反应的都是按钮
按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置
二、按钮的三种状态
normal(普通状态)
默认情况(Default)
对应的枚举常量:UIControlStateNormal
highlighted(高亮状态)
按钮被按下去的时候(手指还未松开)
对应的枚举常量:UIControlStateHighlighted
disabled(失效状态,不可用状态)
如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击
对应的枚举常量:UIControlStateDisabled
三、注意点
(1)从Xcode5开始,图片资源都放到Images.xcassets中进行管理,可以使用拖拽的方式添加项目中用到的图片到Images.xcassets中
(2)若干多个控件共用一段代码,通常使用tag。
四、代码示例
(1)
1 #import "LFViewController.h"
2
3 @interface LFViewController ()
4
5 @property (weak, nonatomic) IBOutlet UIButton *headImageView;
6
7 @end
8
9 @implementation LFViewController
10
11 // 在OC中,绝大多数的控件的监听方法的第一个参数就是控件本身
12 //- (IBAction)left:(UIButton *)button {
13 //
14 // NSLog(@"----");
15 //}
16 - (IBAction)move
17 {
18 // 通过frame修改head的位置
19 // 在OC中,不允许直接修改“对象”的“结构体属性”的“成员”
20 // 允许修改“对象”的“结构体属性”
21 // 1. 取出结构体属性
22 CGRect rect = self.headImageView.frame;
23 // 2. 修改结构体成员
24 rect.origin.y -= 20;
25 // 3. 设置对象的结构体属性
26 self.headImageView.frame = rect;
27 }(2)
1 #import "LFViewController.h"
2
3 /**
4 使用git
5
6 1. 创建项目时,勾选git
7 2. 开发告一段落后,选择"Source Control""Commit",并编写注释
8 */
9
10
11 // 枚举类型实质上就是一个整数,作用就是用来替代魔法数字
12 // 枚举类型中,指定了第一个整数之后,后面的数字会递增
13 typedef enum
14 {
15 kMovingDirTop = 10,
16 kMovingDirBottom,
17 kMovingDirLeft,
18 kMovingDirRight,
19 } kMovingDir;
20
21 #define kMovingDelta 50
22
23 @interface LFViewController ()
24
25 @property (weak, nonatomic) IBOutlet UIButton *headImageView;
26
27 @end
28
29 @implementation LFViewController
30
31 - (IBAction)move:(UIButton *)button
32 {
33 // CGRect rect = self.headImageView.frame;
34 CGPoint p = self.headImageView.center;
35
36 // magic number魔法数字,其他程序员看到代码的时候,不知道是什么意思
37 switch (button.tag) {
38 case kMovingDirTop:
39 p.y -= kMovingDelta;
40 break;
41 case kMovingDirBottom:
42 p.y += kMovingDelta;
43 break;
44 case kMovingDirLeft:
45 p.x -= kMovingDelta;
46 break;
47 case kMovingDirRight:
48 p.x += kMovingDelta;
49 break;
50 }
51
52 [UIView beginAnimations:nil context:nil];
53 [UIView setAnimationDuration:1.0];
54
55 self.headImageView.center = p;
56
57 [UIView commitAnimations];
58 }
59
60 - (IBAction)zoom:(UIButton *)button
61 {
62 CGRect rect = self.headImageView.bounds;
63
64 // 在C语言中,关于bool的判断:非零即真
65 if (button.tag) {
66 rect.size.width += 50;
67 rect.size.height += 50;
68 } else {
69 rect.size.width -= 50;
70 rect.size.height -= 50;
71 }
72
73 // 首尾动画
74 // beginAnimations表示此后的代码要“参与到”动画中
75 [UIView beginAnimations:nil context:nil];
76 [UIView setAnimationDuration:2.0];
77
78 self.headImageView.bounds = rect;
79 // self.headImageView.alpha = 0;
80
81 // commitAnimations,将beginAnimation之后的所有动画提交并生成动画
82 [UIView commitAnimations];
83 }
84
85 @end五、补充笔记
1. IBAction的参数
- (IBAction)left:(UIButton *)button
(1) 在OC中,绝大多数的控件监听方法的第一个参数就是控件本身
(2) 默认连线时的参数类型是id
(3) 如果要在监听方法中,方便控件的使用,可以在连线时或者连线后,修改监听方法的参数类型
2. 修改对象的结构体成员
在OC中,不允许直接修改“对象”的“结构体属性”的“成员”,但是允许修改“对象”的“结构体属性”
修改结构体属性的成员方法如下:
(1)使用临时变量记录对象的结构体属性
(2) 修改临时变量的属性
(3)将临时变量重新设置给对象的结构体属性
3. 在程序开发中需要避免出现魔法数字(Magic Number)
使用枚举类型,可以避免在程序中出现魔法数字
(1)枚举类型实质上就是一个整数,其作用就是用来替代魔法数字
(2)枚举类型中,指定了第一个整数之后,后面的数字会递增
4. frame & bounds & center
1> frame可以修改对象的位置和尺寸
2> bounds可以修改对象的尺寸
3> center可以修改对象的位置
5. 首尾式动画
// beginAnimations表示此后的代码要“参与到”动画中
[UIView beginAnimations:nil context:nil];
// setAnimationDuration用来指定动画持续时间
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
......
// commitAnimations,将beginAnimation之后的所有动画提交并生成动画
[UIView commitAnimations];
- UIUtton 常见属性
- UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
button4.frame = CGRectMake(50, 250, 100, 50);button4.backgroundColor = [UIColor cyanColor];
// 自定义类型, 文字默认是白色;
[button4 setTitle:@"自定义类型" forState:UIControlStateNormal];
// 设置高亮状态下的文字:
[button4 setTitle:@"高亮状态" forState:UIControlStateHighlighted];
// 设置高亮状态下文字的颜色:
[button4 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
// 添加目标事件:
// 当第四个button的touchUpInside事件响应时,会调用clickBtn:方法
[button4 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
// button4有添加了一个响应事件
[button4 addTarget:self action:@selector(clickBtn4:) forControlEvents:UIControlEventTouchDragOutside];
// 设置弧度:
button4.layer.cornerRadius = 15.0;
// 边框的宽度:
button4.layer.borderWidth = 5.0;
// 边框的颜色:(ps: color的类型需要转成CGColor)
button4.layer.borderColor = [[UIColor blackColor] CGColor];
[self.window addSubview:button4];
- // UIControlStateNormal = 0,
- // UIControlStateHighlighted = 1 << 0, // used when UIControl
- // UIControlStateDisabled = 1 << 1,
- // UIControlStateSelected = 1 << 2,
- [btn3 setBackgroundImage:[UIImage imageNamed:@"MC_Checkbox_Unchecked"] forState:UIControlStateNormal];
[btn3 setBackgroundImage:[UIImage imageNamed:@"MC_checkbox_Checked"] forState:UIControlStateSelected];
// selected 属性切换选择状态的属性,如果selected = YES 是选择状态, 如果selected =NO 是正常状态 - // control 可以让你的控件接收系统提供事件
UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 20, 100, 100)];
control.backgroundColor = [UIColor greenColor]; - // addTarget: 添加目标和事件
// 参数一;目标对象
// 参数二:响应的方法
// 参数三监听事件 - [control addTarget:self action:@selector(clickControl:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:control]; - // UIControlEventTouchDown = 1 << 0, 按下时响应
- // UIControlEventTouchDownRepeat = 1 << 1,
- // UIControlEventTouchDragInside = 1 << 2, 按下在里面松手响应
- // UIControlEventTouchDragOutside = 1 << 3,
- // UIControlEventTouchDragEnter = 1 << 4,
- // UIControlEventTouchDragExit = 1 << 5,
- // UIControlEventTouchUpInside = 1 << 6,
- // UIControlEventTouchUpOutside = 1 << 7,
- // UIControlEventTouchCancel = 1 << 8,
UI Button的更多相关文章
- unity5,UI Button too small on device than in Game View解决办法
假设测试设备为iphone5(横屏).下面说明如何使真机上ui显示效果与Game View中一致. 1,首先Game View左上角屏幕规格选 iPhone 5 Wide (16:9),如图: 2,在 ...
- unity5, UI Button "On Button Down"
unity5自带的UI Button的Inspector面板中只有On Click事件,如果我们想让一个按钮响应On Button Down事件该怎么办呢?方法是: 点Add Component-&g ...
- Unity5UGUI 官方教程学习笔记(三)UI BUTTON
Button Interactable :为了避免与该按钮产生交互,可以设置它为false Transition: 管理按钮在正常情况 ,按下,经过时的显示状态 None 按钮整正常工作 但是在按 ...
- Unity UI on the HoloLens
Following the steps under "Required configuration" will allow Unity UI to continue to work ...
- 集成 Kendo UI for Angular 2 控件
伴随着 Angular 2 的正式 release,Kendo UI for Angular 2 的第一批控件已经发布了,当前是 Beta 版本,免费使用. 官方站点:Kendo UI for Ang ...
- 关于UI系统的问题
function OnGUI(){ GUI.skin = myskin; if(GUILayout.Button("add_component",GUILayout.Height( ...
- cocos2dx 3.x(定时器或延时动作自动调用button的点击响应事件)实现自动内测
// // ATTGamePoker.hpp // MalaGame // // Created by work on 2016/11/09. // // #ifndef ATTGamePoker_h ...
- cocos2dx 3.x(多个按钮button执行同一事件的区分)
// // ATTGamePoker.hpp // MalaGame // // Created by work on 2016/10/18. // // #ifndef ATTGamePoker_h ...
- cocos2dx 3.x(Button传统按钮)
// // ATTLoagingScene.hpp // ATT // // Created by work on 16/10/13. // // #ifndef ATTLoagingScene_hp ...
随机推荐
- json字符串转json对象的方法
在使用$.ajax()方法时,我们可以设置dataType:'json'的参数,便可以拿到后台返回的json数据对应的json对象.但有时,我们拿到的是json字符串,需要将它再转成json对象来使用 ...
- Citrix 服务器虚拟化之八 Xenserver虚拟机模版
Citrix 服务器虚拟化之八 Xenserver虚拟机模版 XenServer与VMware不同,Vmware只能将现有的VM转换成模版,而XenServer具有两种方法:一种是将现有 VM 转换为 ...
- MongoDB Tool
robomongo MongoBooster: [推薦]MongoChef:http://3t.io/mongochef/download/ MongoVUE 是个比较好用的MongoDB客户端,不过 ...
- Unity中对象池的使用
unity中用到大量重复的物体,例如发射的子弹,可以引入对象池来管理,优化内存. 对象池使用的基本思路是: 将用过的对象保存起来,等下一次需要这种对象的时候,再拿出来重复使用.恰当地使用对象池,可以在 ...
- hive数据文件简单合并
MR代码: package merge; import java.io.IOException; import java.util.Iterator; import org.apache.hadoop ...
- SQL查询数据库是否存在
在实际工作中会遇到通过SQL查询数据库是否存在的情况,下面一些语句可以提供一些帮助,本文的语句是在SQL08R2中测试的 1,查询当前数据库服务器所有数据库 select * From master ...
- OC基础(26)
集合对象的内存管理 Copy copy与内存管理 @property中的copy关键字 自定义的类实现copy操作 *:first-child { margin-top: 0 !important; ...
- HIVE配置文件
进入HIVE_HOME/conf 编辑文件hive-site.xml,内容如下:(这是伪分布式模式) 主要声明了以下几个内容: 数据仓库地址 数据库连接地址 数据库连接驱动 数据库连接用户名 数据库连 ...
- js对象1--杂志
1.对象数组 var arr = [ ] //直接量创建 var arr = array() //通过函数创建 2. 变量与对象 var a = 12; //变量,不属于谁(默认 ...
- 【PL/SQL练习】游标cursor :oracle 在执行sql语句时,为sql语句所分配的一个私有的内存区域
隐式游标:一次只能返回一行结果(不需要定义,默认自动建立) 显式游标: 需要开发人员提前定义,可以通过循环的方式处理游标里的sql语句,返回多行结果 隐式游标的属性: sql%rowcou ...