• iOS button总结


    1.  //    设置按钮类型,此处为圆角按钮  
    2.     UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    3.     //    设置和大小  
    4.     CGRect frame = CGRectMake(100.0f, 100.0f, 140.0f, 50.0f);  
    5.     //    将frame的位置大小复制给Button  
    6.     writeButton.frame = frame;  
    7.       
    8. //-----------------------------------------------  
    9.     //  给Button添加标题    
    10.         [writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];  
    11.     //   设置按钮背景颜色   
    12.         writeButton.backgroundColor = [UIColor clearColor];  
    13.     //  设置按钮标题文字对齐方式,此处为左对齐  
    14.     writeButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;  
    15.     //使文字距离做边框保持10个像素的距离。  
    16.       writeButton.contentEdgeInsets = UIEdgeInsetsMake(0,30, 0, 0);  
    17. //----------------------------------------------------  
    18.       
    19. /******************************************************    
    20.  //此处类容目的掩饰代码代码操作按钮一些属性,如果设置按钮背景为图片可以将此处注释取消,注释掉上没横线范围类代码,进行测试 
    21.   
    22.     //    设置按钮背景图片 
    23.     UIImage *image= [UIImage imageNamed:@"background.png"]; 
    24.      
    25.     [writeButton setBackgroundImage:image forState:UIControlStateNormal]; 
    26.     //  按钮的相应事件   
    27.   
    28. *****************************************************/  
    29.     [writeButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];  
    30.       
    31.     [self.view addSubview:writeButton];  
    32. }  

    1. <span style="color:rgb(166,19,144)">typedef</span> <span style="color:rgb(166,19,144)">enum</span> <span style="color:rgb(0,34,0)">{</span>  
    2.    
    3.    UIButtonTypeCustom <span style="color:rgb(0,34,0)">=</span> <span style="color:rgb(36,0,217)">0</span>,      <span style="color:rgb(17,116,10)"><em>// 没有风格</em></span>  
    4.    
    5.    UIButtonTypeRoundedRect,     <span style="color:rgb(17,116,10)"><em>// 圆角风格按钮</em></span>  
    6.    
    7.    UIButtonTypeDetailDisclosure,    <span style="color:rgb(17,116,10)"><em>// </em></span>  
    8.    
    9.    UIButtonTypeInfoLight,       <span style="color:rgb(17,116,10)"><em>// 明亮背景的信息按钮</em></span>  
    10.    
    11.    UIButtonTypeInfoDark,        <span style="color:rgb(17,116,10)"><em>// 黑暗背景的信息按钮</em></span>  
    12.    
    13.    UIButtonTypeContactAdd,      <span style="color:rgb(17,116,10)"><em>//</em></span>  
    14.    
    15. <span style="color:rgb(0,34,0)">}</span> UIButtonType;  

    1.预置按钮类型

      sdk提供了5个预置按钮类型:Detail Disclosure,Info Light,Info Dark,Contact Add,Rounded Rectangle。它们添加到sdk中首先是为了方便苹果公司自己。

      构造方式:[UIButton buttonWithType:UIButtonTypeContactAdd];

     

    2.显示系统私有UIButton风格

         指定 值为100 以上的UIButton的buttonWithType可以得到非公开的按钮风格,像红色按钮,黑色按钮,箭头返回按钮等。

         对于某种风格,可以用[button setTintColor:[UIColor blueColor]];来改变按钮颜色。

         参考 

         http://zhaohaiyang.blog.51cto.com/2056753/756082

     

    3.图片和文字环绕

       UIButtonTypeCustom按钮可以设置title。

       若置title于图像上面时,可使用setBackgroundImage;

       若置title于图像右边时,可使用setImage,且要设置frame宽度大于图像,以能显示出title文字。

       设置titleEdgeInsets可实现文字到图片下方,不过要经过一翻计算。

       setImage的图的Z坐标是最高的。

     

    4.光晕效果

       button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;

       Shows Touch On Highlight (高亮)光晕的大小是55x55像素,大于40x40像素的按钮不能使用该视觉效果。

     

    5.指定目标函数传递的参数问题

      例如 

       [button addTarget:self action:@selector(tableView:accessoryButtonTappedForRowWithIndexPath:)  forControlEvents:UIControlEventTouchUpInside];,

      在执行时,传递给tableView函数的参数类型分别是UIButton类型和UITouchesEvent类型。即不论函数原型是什么,button实际传递的参数类型是固定的。

     

    6.点击测试UIButton响应UIControlEventTouchUpInside事件时,响应点超出了它button的范围。

     

    7.在UIButton中addSubview的问题

       UIView的userInteractionEnabled值默认为YES,必须设置UIButton所有的subview的userInteractionEnabled为NO,才能让UIButton正常响应点击。

       但是如果设置了UIView的setUserInteractionEnabled为NO,其子view都将得不到响应。

     

    8.处理双击问题

       [button addTarget:self action:@selector(onTouchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];

       -(void)onTouchUpInside:(id)sender withEvent:(UIEvent*)event 
    {
        UITouch* touch = [[event allTouches] anyObject];
        NSLog(@"onTouchUpInside tagCount:%d",touch.tapCount);

        //判断点击次数
        if (touch.tapCount == 1) 
        {

           //todo
        }
    }


  • 相关阅读:
    作业2(4)求m和n之间的和
    作业2(3)计算x的n次方
    作业3(6)查询水果的单价。有 4 种水果,苹果(apples)、梨(pears)、桔子(oranges)和葡萄(grapes),
    作业3(5)输入五级制成绩(A-E),输出相应的百分制成绩(0-100)区间,要求使用 switch语句。
    作业3(4)循环输入多个年份 year,判断该年是否为闰年。判断闰年的条件是:能被 4 整除但不能被100 整除,或者能被 400 整除。输入-1退出程序执行
    P39-习题2-5
    P39-习题2-7
    计算N户电费
    P39-习题2-4
    P39-习题2-3
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5155876.html
Copyright © 2020-2023  润新知