1 //
2 // ViewController.m
3 // UIButton详解
4 //
5 // Created by 大欢 on 16/1/21.
6 // Copyright © 2016年 bjsxt. All rights reserved.
7 //
8
9 #import "ViewController.h"
10
11 @interface ViewController ()
12
13 @end
14
15 @implementation ViewController
16
17 - (void)viewDidLoad {
18 [super viewDidLoad];
19
20 // UIButton * btn = [[UIButton alloc] init];//custom类型,一般不使用
21
22 //UIButtonTypeSystem 只能得到图片的轮廓,不能得到纹理
23 UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
24 //不能这么设置按钮颜色
25 // btn.titleLabel.text = @"123";
26 /*
27
28 ypedef NS_OPTIONS(NSUInteger, UIControlState) {
29 UIControlStateNormal = 0, // 常规状态显现
30 UIControlStateHighlighted = 1 << 0, // 高亮状态显现
31 UIControlStateDisabled = 1 << 1, // 禁用的状态才会显现
32 UIControlStateSelected = 1 << 2, // 选中状态
33 UIControlStateApplication = 0x00FF0000, // 当应用程序标志时
34 UIControlStateReserved = 0xFF000000 //为内部框架预留
35 // 后两个可以不管他
36 };
37 */
38
39
40 [btn setTitle:@"正常按钮" forState:UIControlStateNormal];
41 [btn setTitle:@"高亮状态" forState:UIControlStateHighlighted];
42
43 [btn setTitle:@"禁用状态" forState:UIControlStateDisabled];
44 [btn setTitle:@"选中状态" forState:UIControlStateSelected];
45
46
47 [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
48 [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
49 [btn setTitleColor:[UIColor greenColor] forState:UIControlStateDisabled];
50 [btn setTitleColor:[UIColor magentaColor] forState:UIControlStateSelected];
51
52 [btn setImage:[UIImage imageNamed:@"apply_sex_normal"] forState:UIControlStateNormal];
53 [btn setImage:[UIImage imageNamed:@"apply_sex_selected"] forState:UIControlStateSelected];
54
55 [btn setBackgroundImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateSelected];
56
57 btn.backgroundColor = [UIColor grayColor];
58 //设置字体
59 btn.titleLabel.font = [UIFont systemFontOfSize:20];
60
61 // NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30]};
62 // NSAttributedString * att = [[NSAttributedString alloc] initWithString:@"Attributed" attributes:dict];
63 //
64 // [btn setAttributedTitle:att forState:UIControlStateNormal];
65
66 //tintColor只有在systemtype时有效,tintColor是子视图及以上视图的颜色。
67 // btn.tintColor = [UIColor orangeColor];
68
69 //开启禁用状态
70 // btn.enabled = NO;
71
72 //开启选中状态
73 // btn.selected = YES;
74 /*
75
76 iOS UIButton事件:
77 UIControlEventTouchDown
78 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
79 UIControlEventTouchDownRepeat
80 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
81 UIControlEventTouchDragInside
82 当一次触摸在控件窗口内拖动时。
83 UIControlEventTouchDragOutside
84 当一次触摸在控件窗口之外拖动时。
85 UIControlEventTouchDragEnter
86 当一次触摸从控件窗口之外拖动到内部时。
87 UIControlEventTouchDragExit
88 当一次触摸从控件窗口内部拖动到外部时。
89 UIControlEventTouchUpInside
90 所有在控件之内触摸抬起事件。
91 UIControlEventTouchUpOutside
92 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
93 UIControlEventTouchCancel
94 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
95 UIControlEventTouchChanged
96 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
97 UIControlEventEditingDidBegin
98 当文本控件中开始编辑时发送通知。
99 UIControlEventEditingChanged
100 当文本控件中的文本被改变时发送通知。
101 UIControlEventEditingDidEnd
102 当文本控件中编辑结束时发送通知。
103 UIControlEventEditingDidOnExit
104 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
105 UIControlEventAlltouchEvents
106 通知所有触摸事件。
107 UIControlEventAllEditingEvents
108 通知所有关于文本编辑的事件。
109 UIControlEventAllEvents
110 通知所有事件。
111
112
113 */
114 //添加响应事件
115 [btn addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
116
117 // [btn removeTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
118
119
120 // [btn addTarget:self action:@selector(doAction2) forControlEvents:UIControlEventTouchUpOutside];
121 // [btn addTarget:self action:@selector(doAction3) forControlEvents:UIControlEventAllTouchEvents];
122
123
124 // btn.contentEdgeInsets = UIEdgeInsetsMake(50, 50, 50, 50);
125 btn.titleEdgeInsets = UIEdgeInsetsMake(50, 0, 0, 100);
126 btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
127
128 //点击按钮有光照效果
129 // btn.showsTouchWhenHighlighted = YES;
130 //高亮状态按钮颜色是否变深
131 btn.adjustsImageWhenHighlighted = NO;
132
133 btn.frame = CGRectMake(100, 100, 200, 200);
134 [self.view addSubview:btn];
135
136
137 }
138
139 //target - Action
140
141 - (void)doAction:(UIButton *)button {
142
143 NSLog(@"%@",button);
144
145 button.selected = !button.selected;
146
147 }
148
149 - (void)doAction2 {
150
151 NSLog(@"%s",__FUNCTION__);
152
153 }
154 - (void)doAction3 {
155 NSLog(@"%s",__FUNCTION__);
156
157 }
158
159 @end