UIButton - iOS - UI基础知识总结5

来源:互联网 发布:淘宝化妆品名称大全 编辑:程序博客网 时间:2024/05/21 09:36
    // UIButton__________________________________
    
    // UIButton 是iOS中用来响应用户点击的控件,该控件既可以显示文字,也可以显示图片,也可以处理用户交互
    // UIButton 的创建一般采用类方法,UIButton也是UIControl的子类
    
    // 1、创建一个UIButton对象 (类方法创建 不用释放 , alloc创建会出现问题)__________________________________
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];// 默认是UIButtonTypeSystem
    
    /*
     能够定义的button类型有以下6种,
     typedef enum {
            UIButtonTypeCustom = 0, 自定义风格
            UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
            UIButtonTypeInfoLight, 亮色感叹号
            UIButtonTypeInfoDark, 暗色感叹号
            UIButtonTypeContactAdd, 十字加号按钮
     
            UIButtonTypeRoundedRect = UIButtonTypeSystem  圆角矩形
                           
     } UIButtonType;
     */
    
    // 2、设置Button的属性__________________________________
    
    // 设置Button大小
    myButton.frame = CGRectMake(60, 100, 250, 200);
    
    // 设置Button的背景颜色
    myButton.backgroundColor = [UIColor whiteColor];
    
    // 给BUtton设置要显示的文字(可以选择文字的显示格式)
    [myButton setTitle:@"登陆" forState:UIControlStateNormal];
    
    // 设置Button上的字体大小
    myButton.titleLabel.font = [UIFont systemFontOfSize:30];
    
    // 设置Button上的字体的颜色
    myButton.tintColor = [UIColor redColor];
    
    // 设置Button上字体的高亮颜色(设置普通颜色也可以用这个)
    [myButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    
    // 设置按钮按下发光
    myButton.showsTouchWhenHighlighted = YES;
    
    // 设置Button上字体的靠齐方式(默认是居中)
    myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    // 设置Button上文字距离边框的位置
    myButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    // UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
    // UIEdgeInsets insets = {top, left, bottom, right};
    // return insets;
    // }
    
    // 改变圆角
    // 先允许改变、再进行休整
    myButton.layer.masksToBounds = YES;
    myButton.layer.cornerRadius = 10;
    
    // 给Button添加一张正常图片
    [myButton setBackgroundImage:[UIImage imageNamed:@"001.jpg"] forState:UIControlStateNormal];
    
    // 给Button添加一张高亮图片
    [myButton setBackgroundImage:[UIImage imageNamed:@"002.jpg"] forState:UIControlStateHighlighted];
    
    // 给Button添加事件
    [myButton addTarget:self action:@selector(myButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 移除时间
    [myButton removeTarget:self action:@selector(myButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 3、将Button添加到父视图上(类方法创建,不用释放)__________________________________
    [containerView addSubview:myButton];
    
    //UIButton 事件
    /*
     UIControlEventTouchDown
     
     单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
     
     UIControlEventTouchDownRepeat
     
     多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
     
     UIControlEventTouchDragInside
     
     当一次触摸在控件窗口内拖动时。
     
     UIControlEventTouchDragOutside
     
     当一次触摸在控件窗口之外拖动时。
     
     UIControlEventTouchDragEnter
     
     当一次触摸从控件窗口之外拖动到内部时。
     
     UIControlEventTouchDragExit
     
     当一次触摸从控件窗口内部拖动到外部时。
     
     UIControlEventTouchUpInside
     
     所有在控件之内触摸抬起事件。
     
     UIControlEventTouchUpOutside
     
     所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
     
     UIControlEventTouchCancel
     
     所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
     
     UIControlEventTouchChanged
     
     当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
     
     UIControlEventEditingDidBegin
     
     当文本控件中开始编辑时发送通知。
     
     UIControlEventEditingChanged
     
     当文本控件中的文本被改变时发送通知。
     
     UIControlEventEditingDidEnd
     
     当文本控件中编辑结束时发送通知。
     
     UIControlEventEditingDidOnExit
     
     当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
     
     UIControlEventAlltouchEvents
     
     通知所有触摸事件。
     
     UIControlEventAllEditingEvents
     
     通知所有关于文本编辑的事件。
     
     UIControlEventAllEvents
     
     通知所有事件。
    */
0 0