iOS开发之四:常用控件--UIButton的使用

在介绍UIButton的用法前,要先了解一下它的父类UIControl,UIControl是所有具有事件处理功能控件的父类。

而该类控件一般响应事件又有三种形式:基于触摸、基础值、基础编辑。控件的层次关系图如下:

UIControl类的常用方法:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; //添加一个事件  


- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; //删除一个事件

它的子类因为继承了这些方法,所以也经常用到,比如UIButton。

以上两个方法的中最后一个参数,UIControlEvents为点击事件,是枚举类型。

UIControlEventTouchDown                 // 用户按下时触发  

UIControlEventTouchDownRepeat// 点击次数大于1时触发  

UIControlEventTouchDragInside// 当触摸在控件内拖动时触发  

UIControlEventTouchDragOutside// 当触摸在控件之外拖动时触发  

UIControlEventTouchDragEnter// 当触摸从控件外拖动到内部时  

UIControlEventTouchDragExit// 当触摸从控件内拖动到外部时  

UIControlEventTouchUpInside// 在控件内触摸抬起时  

UIControlEventTouchUpOutside// 在控件外触摸抬起时  

UIControlEventTouchCancel// 触摸取消事件,设备被锁上或者电话呼叫打断  


UIControlEventValueChanged// 当控件的值发生改变时  


UIControlEventEditingDidBegin//文本控件开始编辑时  

UIControlEventEditingChanged//文本控件的文本改变  

UIControlEventEditingDidEnd//文本控件结束编辑时  

UIControlEventEditingDidEndOnExit// 文本控件内通过按下回车键结束编辑时  


UIControlEventAllTouchEvents// 所有触摸事件  

UIControlEventAllEditingEvents// 文本编辑的所有事件,for UITextField  

UIControlEventApplicationReserved// range available for application use  

UIControlEventSystemReserved// range reserved for internal framework use  

UIControlEventAllEvents//所有事件 

UIButton

常用方法如下:

- (void)setTitle:(NSString *)title forState:(UIControlState)state;                     // default is nil. title is assumed to be single line  

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;                 // default if nil. use opaque white  

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;           // default is nil. use 50% black  

- (void)setImage:(UIImage *)image forState:(UIControlState)state;                      // default is nil. should be same size if different for different states  

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;            // default is nil  

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; //添加一个事件 

UIControlState也是个枚举类型:

UIControlStateNormal       //正常状态  

UIControlStateHighlighted// 高亮状态  

UIControlStateDisabled//禁用状态  

UIControlStateSelected// 选中状态  

UIControlStateApplication// additional flags available for application use  

UIControlStateReserved   

注意:上面的方法中,setTitle和setImage方法不能同时使用,同时使用,只会显示图片,不显示标题。

如果要设置图片和标题,应该setBackgroundImage和setTitle或者自定义一个UIButton的子类来实现。

UIButton各属性设置的实例代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];  

// 设置frame,与按钮的类型有关  

button.frame = CGRectMake(90, 100, 41, 30);  

// 禁用按钮  

button.enabled = NO;  

// 按钮选中  

button.selected = YES;  

// 设置标题,状态正常  

[button setTitle:@"normal" forState:UIControlStateNormal];  

// 设置标题,状态高亮  

[button setTitle:@"highlighted" forState:UIControlStateHighlighted];  

// 设置标题,状态禁用  

[button setTitle:@"disabled" forState:UIControlStateDisabled];  

// 设置标题,状态选中  

[button setTitle:@"selected" forState:UIControlStateSelected];  

// 设置title的颜色  

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  

//设置标题的字体  

button.titleLabel.font = [UIFont systemFontOfSize:14.0f];  


// 设置背景图片  

[button setBackgroundImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];  

[button setBackgroundImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateHighlighted];  

[button setBackgroundImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateSelected];  


// 设置图片  

[button setImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];  

[button setImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateSelected];  


// 用户在控件内按下抬起时  

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

// 用户按下时  

[button addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchDown];  

// 用户按下时  

[button addTarget:self action:@selector(outside) forControlEvents:UIControlEventTouchUpOutside];  

// 记录用户多次按下  

[button addTarget:self action:@selector(repeat) forControlEvents:UIControlEventTouchDownRepeat];  

// 用户由内向外  

[button addTarget:self action:@selector(dragExit) forControlEvents:UIControlEventTouchDragExit];  

// 用户由外向内  

[button addTarget:self action:@selector(dragEnter) forControlEvents:UIControlEventTouchDragEnter];  

// 事件的取消  

[button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchCancel]; 

注意了,如果想要UIButton的背景图片显示一张网络图片的话,可以用SDWebImage这个开源框架。

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u011619283/article/details/23447903

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容

  • 一、简介 <<UIButton(按钮) : 既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置,实现了监...
    无邪8阅读 5,650评论 0 2
  • 一、UIButton的定义 两种创建方法 1)常规的initWithFrame的方式 UIButton *b...
    西蜀阅读 6,754评论 0 1
  • UIWindow 初始化: self.window= [[UIWindowalloc]initWithFrame:...
    Sunny_Fight阅读 1,036评论 0 1
  • 发现自己喜欢的越来越少了, 都很事情都不在去关注,热闹处也不喜欢去了,朋友说,你越来越像个老年人了。呵呵,也许吧。...
    我是一抹云阅读 314评论 2 2
  • 【《佛经》:彼岸花,开一千年,落一千年,花叶永不相见。情不为因果,缘注定生死。】 花: 有兔茕茕 叶: 跨鲸而来 ...
    有兔茕茕阅读 760评论 20 19