首 页  -  技术分享  - ios学习总结(2) -- UIButton的使用

ios学习总结(2) -- UIButton的使用

分享者:陈辉     2016-03-24

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的内容了