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 ...
随机推荐
- windows service的作成
http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html
- Zabbix监控mysql配置及故障告警配置
本文主要介绍zabbix监控mysql的配置,包含使用zabbix自带模板监控mysql相关信息及自定义key监控mysql同步情况.同时介绍了触发器的创建及zabbix通过邮件方式告警配置. 一.配 ...
- wall 和panel有啥区别
Panel指的是一个面板,用它来对一些控件进行分组,就像组合框控件,即Visual Studio里面用的Group Box Control:而在一些软件界面里面也可以表现为工具条,比如编辑工具条.文件 ...
- SecureCRT的背景和文字颜色的修改
options->;session options->;emulation->;terminal选择linux(相应的服务器系统)ansi color 打上钩options-> ...
- 源码解读—HashMap
什么是HashMap ? hashMap是用什么基础数据结构实现的?HashMap是如何解决hashCode冲突的? hashMap的基础容器是数组+链表(transient Entry[] tabl ...
- (easy)LeetCode 234.Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- ListView中内容的动画效果
LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果,可以在XML文件中设置,亦可以在Java代码中设置. 一种直接在 ...
- Loadrunner 添加windows资源没反应
使用 LoadRunner Controller 添加Windows资源系统没有反应, 解决办法 : 1.关闭Windows 防火墙 2.若使用的不是本机 1) 首先要启动所监测机器的remote r ...
- 举例说明划分子网,路由器IP地址
划分子网的方法是从网络的主机号借用若干位作为子网号,主机号相应地减少了同样的位数.在划分子网前,IP地址是两级结构的:网络号,主机号. 划分子网后,两级IP地址在本单位内部就变为三级IP地址:网络号, ...
- Docker学习总结之docker入门
Understanding Docker 以下均翻译自Docker官方文档 ,转载请注明:Vikings翻译. What is Docker? Docker 是一个开源的平台,设计目标是可以方便开发, ...