zoukankan      html  css  js  c++  java
  • IOS UIButton用法详解

    这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.

      //这里创建一个圆角矩形的按钮
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    // 能够定义的button类型有以下6种,
    // typedef enum {
    // UIButtonTypeCustom = 0, 自定义风格
    // UIButtonTypeRoundedRect, 圆角矩形 
    // UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
    // UIButtonTypeInfoLight, 亮色感叹号
    // UIButtonTypeInfoDark, 暗色感叹号
    // UIButtonTypeContactAdd, 十字加号按钮
    // } UIButtonType;

    //给定button在view上的位置
    button1.frame = CGRectMake(20, 20, 280, 40);

    //button背景色
    button1.backgroundColor = [UIColor clearColor];

    //设置button填充图片
    //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];

    //设置button标题
    [button1 setTitle:@"点击" forState:UIControlStateNormal];

    /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
    //以下是几种状态
    // enum {
    // UIControlStateNormal = 0, 常规状态显现 
    // UIControlStateHighlighted = 1 << 0, 高亮状态显现 
    // UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
    // UIControlStateSelected = 1 << 2, 选中状态 
    // UIControlStateApplication = 0x00FF0000, 当应用程序标志时 
    // UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他 
    // };

    /*
    * 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
    * 那么可以去掉这个功能
    */
    button1.adjustsImageWhenHighlighted = NO;
    /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
    button1.adjustsImageWhenDisabled = NO;
    /* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
    button1.showsTouchWhenHighlighted = YES;

    /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
    按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
    触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
    也可以传入其他类的指针*/
    [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

    //显示控件
    [self.view addSubview:button1];

    注意:

    [button1 addTarget:self 
    action:@selector(alarmTimeDone:) 
    forControlEvents:UIControlEventTouchUpInside
    ];

    addTarget:self 是链接到self,一般都这样设置
    action:@selector(alarmTimeDone:) 时间处理函数
    forControlEvents:UIControlEventTouchUpInside 控件事件处理的消息
     

    //取消按钮已经添加的所有事件:(这个比较重要,若添加了两个事件  两个事件都会被触发)

    [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

    何时释放release UIButton?

    是否在dealloc中对UIButton对象进行release操作,取决于UIButton初始化的方式。

    如果使用[UIButtonbuttonWithType:UIButtonTypeRoundedRect]这种方式,是不需要进行release操作的,因为这种方式是自动释放的。如果使用 [[UIButton alloc]init]的方式,则需要主动进行release释放操作。

     
     
    UIControlEventTouchDown

    单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。

    UIControlEventTouchDownRepeat

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

    UIControlEventTouchDragInside

    当一次触摸在控件窗口内拖动时。

    UIControlEventTouchDragOutside

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

    UIControlEventTouchDragEnter

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

    UIControlEventTouchDragExit

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

    UIControlEventTouchUpInside

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

    UIControlEventTouchUpOutside

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

    UIControlEventTouchCancel

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

    UIControlEventTouchChanged

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

    UIControlEventEditingDidBegin

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

    UIControlEventEditingChanged

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

    UIControlEventEditingDidEnd

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

    UIControlEventEditingDidOnExit

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

    UIControlEventAlltouchEvents

    通知所有触摸事件。

    UIControlEventAllEditingEvents

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

    UIControlEventAllEvents

    通知所有事件。


    实例:    

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1.     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
    2.     // btn.frame = rect_screen;  
    3.     btn.frame = CGRectMake(frame.size.width - 20.0f - 30.0f, frame.size.height - 50.0f, 30.0f, 50.0f);  
    4.     btn.backgroundColor = [UIColor blueColor];  
    5.       
    6.     // UIControlEventTouchDragInside  
    7.     // UIControlEventTouchDragOutside  
    8.       
    9.     [btn addTarget:self action:@selector(dragInside) forControlEvents:UIControlEventTouchDragInside];      
    10.     [btn addTarget:self action:@selector(dragOutside) forControlEvents:UIControlEventTouchDragOutside];      
    11.     // dismissView  
    12.     [btn addTarget:self action:@selector(upInside) forControlEvents:UIControlEventTouchUpInside];      
    13.     [self addSubview:btn];      
    14.     return self;  
    15. }  
    16.   
    17. - (void)dragInside  
    18. {  
    19.     NSLog(@"dragInside...");  
    20. }  
    21.   
    22. - (void)dragOutside  
    23. {  
    24.     NSLog(@"dragOutside...");  
    25. }  
    26. - (void)upInside  
    27. {  
    28.     NSLog(@"upInside...");  
    29. }  

    长按事件

    [objc] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];  
    2.     [aBtn setFrame:CGRectMake(40, 100, 60, 60)];  
    3.     [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"]forState:UIControlStateNormal];  
    4.     //button点击事件  
    5.     [aBtn addTarget:self action:@selector(btnShort:)forControlEvents:UIControlEventTouchUpInside];  
    6.     //button长按事件  
    7.     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(btnLong:)];   
    8.     longPress.minimumPressDuration = 0.8; //定义按的时间  
    9.     [aBtn addGestureRecognizer:longPress];  
    10.   
    11. -(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{  
    12.     if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {  
    13.         NSLog(@"长按事件");  
    14.         UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"确定删除该模式吗?" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil nil];  
    15.         [alert show];  
    16.     }  
    17. }  
  • 相关阅读:
    shell脚本中使用nohup执行命令不生效
    【异常】Could not find artifact com.wm.****:
    【异常】The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    MySQL添加唯一索引
    MacBook Pro实现共享屏幕(多台mac之间)
    【异常】lockfile.AlreadyLocked: ~/airflow/airflow-scheduler.pid is already locked
    CentOS7.2安装Airflow
    Docker
    Docker
    Docker
  • 原文地址:https://www.cnblogs.com/wangjuneng/p/4551582.html
Copyright © 2011-2022 走看看