博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios学习总结(2) -- UIButton的使用
阅读量:6227 次
发布时间:2019-06-21

本文共 4857 字,大约阅读时间需要 16 分钟。

UIButton的类是一个UIControl子类,它实现了在触摸屏上的按钮。触摸一个按钮拦截事件和动作消息发送到目标对象时,它的挖掘。设定的目标和行动方法都继承自UIControl。这个类提供了方法来设置标题,图像,按钮等外观属性。通过使用set方法,你可以指定一个不同的外观为每个按钮状态。

UIButton的定义:

 UIButton *button=[UIButton buttonWithType:(UIButtonType)];

 其中UIButtonType的定义如下:

  typedef NS_ENUM(NSInteger, UIButtonType) {    UIButtonTypeCustom = 0,                         // 无类型(自定义类型)    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // 标准类型    UIButtonTypeDetailDisclosure,  //蓝色小箭头按钮    UIButtonTypeInfoLight, //亮色感叹号    UIButtonTypeInfoDark, //暗色感叹号    UIButtonTypeContactAdd, //十字加号    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // 圆角矩形};

 

button的属性:

- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;                     // 设置标题,即button上显示的文字- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // 设置标题的颜色- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // 设置标题阴影颜色- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;                      // 设置图片- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // 设置背景图片- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // 设置属性名称

 

 

其中forState这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

以下是forState的定义:

typedef NS_OPTIONS(NSUInteger, UIControlState) {    UIControlStateNormal       = 0,                       // 正常状态显示    UIControlStateHighlighted  = 1 << 0,                  // 高亮状态显示    UIControlStateDisabled     = 1 << 1,                  // 禁用状态显示    UIControlStateSelected     = 1 << 2,                  // 选中状态显示    UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use};

 

以下属性是设置forState中的各种状态是否启用:

@property(nonatomic)          BOOL         reversesTitleShadowWhenHighlighted; // default is NO. if YES, shadow reverses to shift between engrave and emboss appearance@property(nonatomic)          BOOL         adjustsImageWhenHighlighted;    // default is YES. if YES, image is drawn darker when highlighted(pressed)@property(nonatomic)          BOOL         adjustsImageWhenDisabled;       // default is YES. if YES, image is drawn lighter when disabled@property(nonatomic)          BOOL         showsTouchWhenHighlighted __TVOS_PROHIBITED;      // default is NO. if YES, show a simple feedback

给button添加事件:

[button1 addTarget:self action:@selector(callback:) forControlEvents:UIControlEventTouchUpInside];- (IBAction)callback:(id)sender;{...}

以下是button的事件:

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.    UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13,     // semantic action: for buttons, 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};

显示button:

[self.view addSubview: button]

 

以上就是UIButton的内容了

转载于:https://www.cnblogs.com/chfight/p/5320071.html

你可能感兴趣的文章
IT架构之IT架构标准——思维导图
查看>>
ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题...
查看>>
List、Map和Set实现类
查看>>
Android Fragment 真正彻底的解决(下一个)
查看>>
zoj 3659 并检查集合
查看>>
VS2010如何调试IIS上的网站
查看>>
Codeforces 327B-Hungry Sequence(素数筛)
查看>>
iPhone 6/plus iOS Safari fieldset border 边框消失
查看>>
Xms Xmx PermSize MaxPermSize 区别
查看>>
Appium for win7 环境搭建
查看>>
【转载】MFC动态创建控件及其消息响应函数
查看>>
解決BufferedReader读取UTF-8文件中文乱码(转)
查看>>
【转】预装(push)lib64中so文件查找错误
查看>>
2014百度之星预赛(第二场)——Best Financing
查看>>
《Python简明教程》总结
查看>>
构造 - HDU 5402 Travelling Salesman Problem
查看>>
[转]图解分布式一致性协议Paxos
查看>>
【SSH2(实用文章)】--Struts2文件上传和下载的例子
查看>>
Rust初步(七):格式化
查看>>
maven教程
查看>>