iOS之UIButton的各种使用

来源:互联网 发布:淘宝有卖烟的吗 编辑:程序博客网 时间:2024/05/16 15:03
////  ViewController.m//  Day2ClassCode1////  Created by Leven on 15/7/21.//  Copyright (c) 2015年 Leven. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    // 我们创建一个Button,给定一个坐标,以及大小    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(20, 100, 100, 50)];    // 给btn 添加一个背景颜色    btn.backgroundColor=[UIColor yellowColor];    // 普通模式是下边,我的按钮标题是"按钮"    [btn setTitle:@"按钮" forState:UIControlStateNormal];    // 在普通模式下边,给按钮的字添加颜色    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//    UIControlStateNormal     这表示Btn 是在通常模式下边//    UIControlStateHighlighted  这个表示Btn是在高亮状态下//    UIControlStateDisabled     这个是表示Btn 未选择状态//    UIControlStateSelected     这个是表示Btn 在选择状态        // 给按钮添加一个事件 touchEvent    [btn addTarget:self action:@selector(touchEvent:) forControlEvents:UIControlEventTouchUpInside];    // 给按钮添加一个touchDown方法,在Touch Down(按下)的时候触发,    [btn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];    // 给按钮添加一个touchDragOutSide方法,在按钮外拖动取消    [btn addTarget:self action:@selector(touchDragOutSide:) forControlEvents:UIControlEventTouchDragOutside];    // 给按钮添加一个事件,在按钮里边拖动的时候    [btn addTarget:self action:@selector(touchDragInSide:) forControlEvents:UIControlEventTouchDragInside];        [btn addTarget:self action:@selector(dragEnter:) forControlEvents:UIControlEventTouchDragEnter];//    1、addTarget 这个是表示对那个控制器添加一个事件,也可以理解为,按钮的事件在那里触发//    2、这个直接理解为我的btn 添加事件的方法名,如果后边带了冒号就表示需要带参数,这个参数就是我的btn 本身,如果不加冒号,就表示不带任何参数的//    3、//    UIControlEventTouchDown              当按钮按下的时候,还未抬起  UIControlEventTouchDownRepeat          当按钮触控点不止一个的时候//    UIControlEventTouchDragInside        在按钮里边拖动的时候//    UIControlEventTouchDragOutside       当在按钮外边拖动的时候//    UIControlEventTouchDragEnter         当//    UIControlEventTouchDragExit          当按钮在外边想要退出事件的时候//    UIControlEventTouchUpInside          当按钮抬起的时候//    UIControlEventTouchUpOutside         当在按钮外边抬起的时候//    UIControlEventTouchCancel            当在外边拖动直接取消掉,类似OutInside        //设置按钮是否可被点击    btn.enabled = YES;        //设置按钮点击(高亮)下字体颜色    [btn setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];            // 把刚创建的按钮加到界面上;    [self.view addSubview:btn];}- (void)dragEnter:(UIButton *)Btn{    NSLog(@"dragEnter");}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark ---按钮事件- (void)touchDragInSide:(UIButton *)btn{//    NSLog(@"在按钮里边拖动");}- (void)touchDragOutSide:(UIButton *)btn{//    NSLog(@"我在按钮外面滑动");    }- (void)touchDown:(UIButton *)btn{//    NSLog(@"我的按钮按下了");        }// 按钮触发的事件- (void)touchEvent:(UIButton *)btn{    //    [btn setTitle:@"我点击了" forState:UIControlStateNormal];//    NSLog(@"我点击了我的按钮");}@end

0 0
原创粉丝点击