UIButton总结

1.按钮控件能够与用户进行交户 ,点击按钮能够进行页面切换操作

    // <1>创建按钮对象

    /*

     UIButtonTypeCustom    自定义类型  按照用户需求任意修改按钮的样式

     UIButtonTypeSystem      xcode5.0 出现新的样式

     

     UIButtonTypeRoundedRect = UIButtonTypeSystem,   System样式替代了RoundedRec  所UIButtonTypeRoundedRect 已经不在具有圆角边框效果 如果想实现圆角边框效果必须借助层实现

     

     

     // 系统样式  不需要添加标题和图片   即使添加也显示不出来

     UIButtonTypeDetailDisclosure,

     UIButtonTypeInfoLight,

     UIButtonTypeInfoDark,

     UIButtonTypeContactAdd,

     */

    UIButton * brn1 = [UIButton buttonWithType:UIButtonTypeCustom];

 

 

    // <2>按钮添加背景颜色

    // custom样式的按钮的背景颜色 默认为无色

    

    brn1.backgroundColor = [UIColor redColor];

 

 

//<3>为按钮添加标题

    /*

     

     UIControlStateNormal   常态 对按钮不做任何操作状态

     UIControlStateHighlighted   高亮状态    长按  按钮不抬起的状态

     UIControlStateDisabled       失效状态       按钮不能做任何操作

     UIControlStateSelected      按钮被选中得状态

     */

    [brn1 setTitle:@"点我呀~" forState:UIControlStateNormal];

 

 

 

 // <4>为按钮上得标题添加颜色

    // custom 样式的按钮默认为白色

    

    [brn1 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

    

    // <5>设置按钮的显示位置

    brn1.frame = CGRectMake(150, 50, 100, 30);

    

    // <6>设置按钮上标题的文字大小

    brn1.titleLabel.font = [UIFont systemFontOfSize:24];

 

// <5>设置按钮的显示位置

    brn1.frame = CGRectMake(150, 50, 100, 30);

 

    // <6>设置按钮上标题的文字大小

    brn1.titleLabel.font = [UIFont systemFontOfSize:24];

 

    // <7>设置按钮 高亮状态文字的内容和文字的颜色

    [brn1 setTitleColor:[UIColor  blueColor] forState:UIControlStateHighlighted];

 

// <8>为按钮添加背景图片

 

    // [特点] 如果图片的大小大于按钮的大小 那么图片就会按照按钮的宽进行压缩  如果图片的大小小于 按钮的大小 那么图片就会按照按钮的宽高进行扩展

    // 2.使用setBackgroundImage:为按钮添加背景图片 文字会悬浮在图片之上

    

    // 图片类就是 UIImage

    // 创建图片的对象指针

    UIImage *image = [UIImage imageNamed:@"010.png"];

    // 图片的后缀不能缺省 如果图片的后缀为.png的可以省略 但是其它类的图片的后缀一定不能缺省 如果缺省就找不到图片了

       [btn1 setBackgroundImage:image forState:UIControlStateNormal];

       // 设置按钮高亮状态下得图片

    [btn1 setBackgroundImage:[UIImage imageNamed:@"011.png"] forState:UIControlStateHighlighted];

    [self.window addSubview:btn1];

 

 <2>setImage:

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];

    btn2.frame = CGRectMake(150, 220, 150, 100);

    [btn2 setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];

    [btn2 setTitle:@"setImage" forState:UIControlStateNormal];

     //[特点]如果图片的大小 大于按钮的大小 那么图片就不按照按照的大小进行压缩 如何图片大大小 小于按钮的大小 那么图片就会 原样显示

    // 2. setImage添加图片 文字信息显示在图片右侧的空白区域

      [btn2 setImage:[UIImage imageNamed:@"012.png"] forState:UIControlStateNormal];

    [btn2 setImage:[UIImage imageNamed:@"014.png"] forState:UIControlStateHighlighted];

    [self.window addSubview:btn2];

 

 

 

    // 制作图片按钮

    UIImage *imge = [UIImage imageNamed:@"015.png"];

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];

    button3.backgroundColor = [UIColor brownColor];

    // 设置按钮的大小和图片的大小相同

    button3.frame = CGRectMake(150, 300, imge.size.width, imge.size.height);

    [button3 setImage:imge forState:UIControlStateNormal];

     [self.window addSubview:button3];

 

 

    // system样式的按钮

    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeSystem];

    button4.frame = CGRectMake(140, 340, 100, 100);

    [button4 setTitle:@"system" forState:UIControlStateNormal];

    [button4 setImage:[UIImage imageNamed:@"017.png"] forState:UIControlStateNormal];

    // [特点]只取图片轮廓 不取图片内容 默认填充颜色为蓝色

    [button4 setTintColor:[UIColor purpleColor]];  // 设置轮廓颜色

      [button4 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal ];

    [self.window addSubview:button4];

 

// 添加按钮事件

    // 为按钮添加点击事件

    // [注意]按钮的点击事件必须借助与addTarget 将点击的按钮与触发的事件进行关联

    /*

     1.对象指针 action后面的方法在那个类实现此处就写那个类的对象指针 self

     2.点击按钮触发方法 (按钮触发方法带有形参的 所以该方法名冒号不能缺省)

     3.按钮的触发事件 (以什么方式对按钮进行操作)

     */

    [btn1 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];

 

    

    

    // 为按钮添加 边框及圆角效果

    btn2.layer.masksToBounds = YES;

    btn2.layer.borderWidth = 2; // 边框的宽度

    btn2.layer.cornerRadius = 8;// 设置圆角效果

    

    [self.window addSubview:btn2];

    

}

-(void)pressBtn:(id)sender

 

{

    // id 类型代表UIButton类型

    // sender代表触发该方法按钮的指针

    

    // <1>获取点击的按钮

    UIButton *tempBtn = (UIButton *)sender;

    if (tempBtn.tag == 100) {

        self.window.backgroundColor = [UIColor redColor];

    }else{

        static int count = 10;

        count ++;

        self.window.backgroundColor = [UIColor colorWithRed:(count *10 %255)/255.0 green:(count *20 %255)/255.0 blue:(count *30 %255)/255.0 alpha:1];

    }

}

 

 

    // <3>获取点击的按钮 上面的按钮是否为空

    NSString *title = [btn currentTitle];  // 获取当前按钮的标题

currentColor = [btn currentTitleColor];  // 获取按钮上标题的颜色

 

 

// 添加计数起

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeNum) userInfo:nil repeats:YES];

      //关闭时间计时器

        [timer invalidate];

 

 

posted @ 2015-08-23 22:09  BN笨的很想飞  阅读(272)  评论(0编辑  收藏  举报