前面写了UIWindow、UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实现是使用系统提供的各种控件,其他的就是自定义实现了,作者目前是入门状态,只能写写基础控件了。

iOS中提供了UIButton、UILable、UITextField、UIImageView等基础UI控件,继承于UIView。这里先拿UIButton练练手,为什么拿UIButton呢,因为UIbutton继承自UIControl,UIControl派生自UIView类,每个控件都有很多视图的特性,包括附着于其他视图的能力,所有控件都拥有一套共同的属性和方法,包含显示内容,点击事件等等,UIControl的子类都有事件处理能力。

图、UIControl

UIButton的定义:

UIButton可使用 initWithFrame、buttonWithType两种方式创建:

1)initWithFrame

  UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(60, 60, 200, 60)];
btn.backgroundColor=[UIColor greenColor];
[btn setTitle:@"btn1" forState:UIControlStateNormal];
[self.view addSubview:btn];

2)buttonWithType

UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect rect=CGRectMake(60, 160, 200, 60);
btn2.frame=rect;
btn2.tag=1001;
btn2.backgroundColor=[UIColor colorWithRed:30/255.0 green:200/255.0 blue:125/255.0 alpha:1.0];
[btn2 setTitle:@"btn2" forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(btn2Pressed) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn2];

btn2Pressed方法:

-(void)btn2Pressed{

    NSLog(@"button pressed");
}

UIButtonType:

typedef enum {
UIButtonTypeCustom = 0, // no button type 自定义,无风格
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片
UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁
UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁
UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮
UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁
} UIButtonType;

UIButton常用属性:

//设置对应状态的标题内容default is nil. title is assumed to be single line

- (void)setTitle:(NSString *)title forState:(UIControlState)state;

//设置对应状态的标题颜色

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

//设置对应状态的标题阴影颜色

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;

//设置对应状态的按钮的图片

- (void)setImage:(UIImage *)image forState:(UIControlState)state;

//设置对应状态的按钮背景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

UIButton的UIControlState   :

typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};

更多属性可参考官方文档。

UIButton添加事件:

UIButton使用如下方法添加事件。
[btn addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>]

这些事件都是基于触摸、基于值、基于编辑。可相应如下事件。

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // on all touch downs
UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8, UIControlEventValueChanged = 1 << 12, // sliders, etc. UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing UIControlEventAllTouchEvents = 0x00000FFF, // for touch events
UIControlEventAllEditingEvents = 0x000F0000, // for UITextField
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents = 0xFFFFFFFF
};
/**
* @author 张兴业

*  iOS入门群:83702688
*  android开发进阶群:241395671
*  我的新浪微博:@张兴业TBOW
*/

参考:

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/Introduction/Introduction.html#//apple_ref/doc/uid/TP40006955-CH1-SW1

一、UIKit结构图

iOS学习——UI基础UIButton(七)的更多相关文章

  1. iOS开发——UI基础-UIButton、UIImageView、UILabel的选择

    1.UILabel - UILabel的常见属性 @property(nonatomic,copy) NSString *text; 显示的文字 @property(nonatomic,retain) ...

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

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

  3. UI基础UIButton

    UI基础UIButton 前面写了UIWindow.UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实 ...

  4. IOS开发UI基础--数据刷新

    IOS开发UI基础--数据刷新 cell的数据刷新包括下面几个方面 加入数据 删除数据 更改数据 全局刷新方法(最经常使用) [self.tableView reloadData]; // 屏幕上的全 ...

  5. iOS学习笔记(七)——UI基础UIButton

    前面写了UIWindow.UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实现是使用系统提供的各种控件 ...

  6. iOS开发-UI基础Demo

    现在更多的学习资料都是xCode4.X的,发现xCode6.1还是很多东西,如果有正在学习iOS开发的可以通过Demo简单了解下iOS的UI开发~ 1.新建单视图文件: 2.新建项目名称,语言选择OC ...

  7. UI基础:UIButton.UIimage 分类: iOS学习-UI 2015-07-01 21:39 85人阅读 评论(0) 收藏

    UIButton是ios中用来响应用户点击事件的控件.继承自UIControl 1.创建控件 UIButton *button=[UIButton buttonWithType:UIButtonTyp ...

  8. IOS开发UI基础之UIButton

    什么是按钮?

  9. IOS开发UI基础学习-------总结

    什么叫控件? 屏幕上所有UI元素都叫做控件(也有叫做视图.组件) 控件的共同属性 尺寸 位置 背景色 ... 苹果将控件的共同属性都抽取到父类UIView中 所有控件最终都继承自UIView 父子控件 ...

随机推荐

  1. A - Prime Ring Problem(素数环,深搜,打表)

    Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into ...

  2. 巡逻机器人(BFS)

    巡逻机器人问题(F - BFS,推荐) Description   A robot has to patrol around a rectangular area which is in a form ...

  3. datetime.timedelta

    from django.utils import timezoneimport datetime timezone.now()datetime.datetime(2014, 7, 18, 9, 42, ...

  4. windows无效字符名导致的错误及解决办法

    今天用file_put_content($fileName,$data)产生错误:内容如下: Warning: file_put_contents(images/7d5636992a7395f9174 ...

  5. 一步一步重写 CodeIgniter 框架 (12) —— 代码再重构,回归 CI

    第一课中搭建的基本的 框架模型, 只有一个 index.php 作为执行文件,按这种方式最不稳定的因素就是路径的问题. 我们经常需要通过合适的参数,比如 load_class('output') 或 ...

  6. 更新整理本人全部博文中提供的代码与工具(Java,2014.09)

    为了更方便地管理博文中涉及的各种代码与工具资源,如今把这些资源迁移到 GitHub 中,有兴趣者可前往下载. Java 1.<高效 Java Web 应用开发框架 JessMA v3.4.1 正 ...

  7. WinForm 小程序 NotePad

    运行效果: 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  8. android4.0移植,拨号异常

    D/dalvikvm( 2274): GC_CONCURRENT freed 206K, 12% free 6571K/7431K, paused 2ms+3ms D/dalvikvm( 2274): ...

  9. 新浪微博中tableview中头部信息

    摘自http://www.cnblogs.com/gcb999/p/3151665.html #import <UIKit/UIKit.h> @class User; @protocol ...

  10. SuperSocket源码解析之会话生命周期

    一 基本概念 会话(Session)是客户端与服务器进行通信的基本单元,也是一个Socket的封装,在http协议中也有Session机制,其主要作用封装一个通信单元socket,负责服务器与客户端消 ...