UIButton的一些基本用法

来源:互联网 发布:淘宝口令红包怎么抢 编辑:程序博客网 时间:2024/05/31 04:03

一:基本知识

    UIButton:UIControl:UIView 表示继承都关系. 所有事件驱动型的控件,父类都为UIControl,使用UIButton,可以使用户和APP进行交互类方法创建一个UIButton type button的样式.iOS7之后, UIButtonTypeRoundRect(创建圆角按钮)无效果

    1:工厂方法,不需要实例化

         能够定义的button类型有以下6种,

        typedef enum {

            UIButtonTypeCustom = 0,          自定义风格

            UIButtonTypeRoundedRect,         圆角矩形

            UIButtonTypeDetailDisclosure,    蓝色小箭头按钮,主要做详细说明用

            UIButtonTypeInfoLight,           亮色感叹号

            UIButtonTypeInfoDark,            暗色感叹号

            UIButtonTypeContactAdd,          十字加号按钮

       } UIButtonType;


    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    通过点语法设置相应的UIButton对象btn的大小和背景颜色

    btn.backgroundColor = [UIColorredColor];

    btn.frame =CGRectMake(10,30,300,30);

    设置标题文字。UIControlStateNormal正常状态显示。

         

    forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

    以下是几种状态

       enum {

            UIControlStateNormal       =0,        常规状态显现

            UIControlStateHighlighted  =1 <<0,   高亮状态显现

            UIControlStateDisabled     =1 <<1,   禁用的状态才会显现

            UIControlStateSelected     =1 <<2,   选中状态

            UIControlStateApplication  =0x00FF0000,当应用程序标志时

            UIControlStateReserved     =0xFF000000 为内部框架预留,可以不管他

        };

    [btn setTitle:@"圆角按钮"forState:UIControlStateNormal];

    设置标题文字颜色

    [btn setTitleColor:[UIColoryellowColor]forState:UIControlStateNormal];

    被点击之后成高亮状态。

    [btn setTitle:@"圆角按钮被点击"forState:UIControlStateHighlighted];

    设置字体的大小

    btn.titleLabel.font = [UIFontsystemFontOfSize:14.0];

        

 

    有些时候我们想让UIButtontitle居左对齐,我们设置

    //btn.textLabel.textAlignment = UITextAlignmentLeft ;是没有作用的,我们需要设置

    btn.contentHorizontalAlignment =UIControlContentHorizonAlignmentLeft;

    //但是问题又出来,此时文字会紧贴到做边框,我们可以设置

    btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

    //使文字距离做边框保持10个像素的距离。


    

    2:事件驱动型控件的体现

    UIControlEventTouchUpInside(按住Button并且btnFrame的范围之内松开手指)

    target-action: 满足特定事件后,会让target对象来执行action方法,即target-执行某一个对象中的方法。action-要执行的方法名。forControlEvents触发事件的形式。所以必须实现btnClicked:方法UIControlEventTouchUpInside表示点击按钮。

    注意:action方法在target中必须存在,否则,程序会崩溃,方法加上:会将btn本身作为参数传递过去

    1:使用该方法

    [btn addTarget:selfaction:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];

    

    2:通过创建一个类实现相应的方法,可以调用该类的对象和方法,不仅仅使局限与本类。每按一次按钮,都会打印MyClass中的report方法执行。

    MyClass *myclass=[[MyClassalloc]init];

    [btn addTarget:myclassaction:@selector(report)forControlEvents:UIControlEventTouchUpInside];

    //表示当被点击当时候会成高亮状态,中间会发,showsTouchWhenHighlighted默认情况下为NO.

    btn.showsTouchWhenHighlighted=YES;

    //设置标记

    btn.tag =100;

    

    

    3:

    开发中,UIButton,会加上贴图使用.UIButtonTypeCustom贴图的Button对应的固定样式(自定义样式)

    UIButton *customBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

   

    UIImage图片类 (根据图片的名称,得到图片对象),要把图片显示出来首先要把图片加载到supporting Files中,然后通过UIImage创建图片对象,通过加入对应到图片名。再通过setImage setBackgroundImage方法使图片加载到按钮对象当中。

   UIImage *image = [UIImageimageNamed:@"1.png"];

   UIImage *bgimage = [UIImageimageNamed:@"map.png"];

    

    设置图片   setImage setBackgroundImage区别:前者的贴图显示在上面,setImage btn很大时,图片不会被拉伸;setBackgroundImage图片的大小会随着btnframe而变化

    [customBtn setImage:imageforState:UIControlStateNormal];

    [customBtn setBackgroundImage:bgimageforState:UIControlStateNormal];

   

二:常用知识

1)设置button为圆角方法

    

    button.layer.cornerRadius=button.frame.size.height/2;//自身高度的一半.如果是正方形,那么剪裁后为一个园。其他形状可根据需求设置圆角半径大小

    button.layer.masksToBounds=YES;

2).设置边界距离 
  contentEdgeInsets  property // 按钮content内外线边缘绘制区域.content包含按钮图片和标题 
  titleEdgeInsets  property // 按钮标题绘制区域控制. 
  imageEdgeInsets  property // 图片绘制偏移区域控制. 
. 
 
3.得到当前状态 
  currentTitle  property // 当前按钮上显示的标题(只读),当按钮状态改变时值自动改变.值可以为nil. 
  currentTitleColor  property // 当前标题颜色(只读).此值要保证不为nil,默认是白色. 
  currentTitleShadowColor  property // 标题的阴影颜色(只读).默认是白色. 
  currentImage  property // 当前按钮上的图片(只读).可以是nil. 
  currentBackgroundImage  property // 当前按钮背景图片(只读).可以是nil. 
  imageView  property // 按钮上的图片视图(只读).虽然它是只读的,但是他的属性是可读写的.imageView的属性在按钮还没有显示前就有值了.系统按钮这些值是nil 
 
4.得到尺寸 
– backgroundRectForBounds: // 返回背景绘制区域. 
– contentRectForBounds: // 返回内容绘制区域.内容区域是显示图片和标题及他们特定对齐缩放等的范围. 
– titleRectForContentRect: // 返回标题的绘制区域. 
– imageRectForContentRect: // 返回图片的绘制区域. 
 
 按钮状态: 
 
UIControlEventTouchDown      // 按下    
UIControlEventTouchDownRepeat  // 多次按下   
UIControlEventTouchDragInside   // 保持按下然后在按钮及其一定的外围拖动 
UIControlEventTouchDragOutside  // 保持按下,在按钮外面拖动 
UIControlEventTouchDragEnter  // DragOutside进入DragInside触发 
UIControlEventTouchDragExit  // in到out触发 
UIControlEventTouchUpInside // 在按钮及其一定外围内松开 
UIControlEventTouchUpOutside // 按钮外面松开 
UIControlEventTouchCancel   // 点击取消 



0 0
原创粉丝点击