iOS UIButton[九宫格]

iOS UIButton[九宫格]_第1张图片
九宫格color.gif
  • UIButton------基础控件
  • 显示文字 显示图片
  • 监听用户的点击事件,在用户点击后做出响应
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIButton : UIControl 

一、定义UIButton + 布局

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(150, 150, 100, 50);    


UIButton * btn1 = [[UIButton alloc]initWithFrame:CGRectMake(150, 150, 100, 50)];

UIButton 类型:

typedef NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom = 0,                         // no button type自定义风格  
    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button

    UIButtonTypeDetailDisclosure,        //蓝色小箭头按钮,主要做详细说明用
    UIButtonTypeInfoLight,        //亮色感叹号
    UIButtonTypeInfoDark,        //暗色感叹号
    UIButtonTypeContactAdd,        //十字加号按钮  
    
    UIButtonTypePlain API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios, watchos), // standard system button without the blurred background view
    
    UIButtonTypeRoundedRect = UIButtonTypeSystem   // Deprecated, use UIButtonTypeSystem instead圆角矩形
};

二、将定义的UIButton添加到父视图上

[self.view addSubview:btn];

三、设置自定义UIButton的各种属性

  • UIButton的文本信息
[btn setTitle:@"一个Button" forState:UIControlStateNormal];
//按钮状态与对应的文本信息
  • UIButton状态
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)
    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
};
  • 背景颜色
btn.backgroundColor = [UIColor redColor];  
[btn setBackgroundColor:[UIColor redColor]];
  • 字体颜色和字体大小
//字体颜色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];  

//字体大小
btn.titleLabel.font = [UIFont fontWithName:@"Arial" size:20];
  • 对齐模式
[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
    UIControlContentHorizontalAlignmentCenter = 0,
    UIControlContentHorizontalAlignmentLeft   = 1,
    UIControlContentHorizontalAlignmentRight  = 2,
    UIControlContentHorizontalAlignmentFill   = 3,
    UIControlContentHorizontalAlignmentLeading  API_AVAILABLE(ios(11.0), tvos(11.0)) = 4,
    UIControlContentHorizontalAlignmentTrailing API_AVAILABLE(ios(11.0), tvos(11.0)) = 5,
};
  • 内部内容边距
[btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
  • 文字内在距离
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  • 设置图片
//设置UIButton填充  
UIImage* image = [UIImage imageNamed:@"btn.png"];
[btn setImage:image forState:UIControlStateNormal];

//背景图片
[btn setBackgroundImage:image forState:UIControlStateNormal];

四、设置UIButton的响应事件

//无参
[btn addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];

//有参
[btn addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];

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
};
//无参
-(void)changeColor{
   self.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
}

//有参
//父视图通过tag值获取子视图的指针对象
-(void)changeColor:(UIButton*)button{
 if (btn.tag == 1) {
   self.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
   }
}

附一个九宫格demo

你可能感兴趣的:(iOS UIButton[九宫格])