iOS学习笔记(3)UIButton

【UIVIew】

//UIView是所有视图的父类,UIView的属性和方法,就是所有视图的属性和方法


【UIButton】

//UIButton是一个视图类,继承自UIControl 间接继承自UIView

//凡是UIControl子类可以接收触发事件

//UIButton用于创建一个按钮,添加点击事件


Button创建两种方式


(1)//创建buttonUI

  UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(50, 50, self.view.frame.size.width-100, 50)];


(2)/设置圆角按钮两种方式 

a.button=[UIButton buttonWithType:UIButtonTypeCustom];

        button.layer.cornerRadius=15;


b.button=[UIButton buttonWithType:UIButtonTypeCustom];

button.layer.cornerRadius=15;

//button设置颜色

  button.backgroundColor=[UIColor redColor];


 //设置文字

    //第一个参数 button文字第二个参数是 button的状态

    /*

    typedef NS_OPTIONS(NSUInteger, UIControlState) {

        UIControlStateNormal      普通状态

        UIControlStateHighlighted 高亮状态

        UIControlStateDisabled    禁用状态

        UIControlStateSelected    选中状态

    };

//*/

    [button setTitle:@"我是按钮" forState:UIControlStateNormal];

    

    [button setTitle:@"高亮" forState:UIControlStateHighlighted];

    

    [button setTitle:@"禁用" forState:UIControlStateDisabled];

    使用禁用状态需要将enabled设为No;

     [button setTitle:@"我被选中了" forState:UIControlStateSelected];

使用选中状态要将selected设置为YES


   

 //设置禁用状态属性,默认为YES 

    button.enabled=NO;

    //设置选中状态属性,默认为no 

   button.selected=YES;


//设置按钮文字颜色

    //普通状态

    [button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];

    //高亮状态下

    [button setTitleColor:[UIColor greenColor] forState:(UIControlStateHighlighted)];

    //禁用状态下文字yanse

    

    [button setTitleColor:[UIColor grayColor] forState:(UIControlStateDisabled)];

    

    //选中状态下文字颜色

    

    [button setTitleColor:[UIColor yellowColor] forState:(UIControlStateSelected)];



//设置button的前景图

    [button setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

    //设置高亮状态下得前景图

    [button setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateHighlighted];

    //设置禁用状态下得前景图

    [button setImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateDisabled];

    //设置选中状态下得前景图

    [button setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateSelected];




//设置button的背景图普通状态下

    [button setBackgroundImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];

    //设置button的背景图高亮状态下

    [button setBackgroundImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateHighlighted];

    //设置禁用状态背景图

    [button setBackgroundImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateDisabled];

    //设置选中状态下得背景图

    [button setBackgroundImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateSelected];



//button添加事件

    /*  id  接收消息的对象

        sel 触发事件的方法

        UIControlEvents 是事件类型


    */

    

     typedef NS_OPTIONS(NSUInteger, UIControlEvents) {

     UIControlEventTouchDown//点击即触发

     UIControlEventTouchDownRepeat//多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。

     UIControlEventTouchDragInside//在按钮里面拖拽触发(走一步触发一次)

     UIControlEventTouchDragOutside//在拖拽道按钮外面触发(走一步触发一次)

     UIControlEventTouchDragOutside//当一次触摸在控件窗口之外拖动时。

     UIControlEventTouchDragEnter//当一次触摸从控件窗口之外拖动到内部时。

     UIControlEventTouchDragExit//当一次触摸从控件窗口内部拖动到外部时。

     UIControlEventTouchUpInside//所有在控件之内触摸抬起事件。

     UIControlEventTouchUpOutside//所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)

     UIControlEventTouchCancel//所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

     UIControlEventTouchChanged//当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

     UIControlEventEditingDidBegin//当文本控件中开始编辑时发送通知。

     UIControlEventEditingChanged//当文本控件中的文本被改变时发送通知。

     UIControlEventEditingDidEnd//当文本控件中编辑结束时发送通知。

     UIControlEventEditingDidOnExit//当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

     UIControlEventAlltouchEvents//通知所有触摸事件。

     UIControlEventAllEditingEvents//通知所有关于文本编辑的事件。

     UIControlEventAllEvents//通知所有事件。

     };

    [button addTarget:self action:@selector(onClick:) forControlEvents:(UIControlEventTouchDown)];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值