代码改变世界

iOS Button的故事

2016-01-14 22:41  000aaa  阅读(167)  评论(0编辑  收藏  举报

UIButton 继承与UIControl (UIControl继承与UIView)

1.基本语法

  1 #import "ViewController.h"
  2 #define WIDTH  self.view.bounds.size.width
  3 @interface ViewController ()
  4 {
  5     NSInteger m_counter;
  6 }
  7 @end
  8 
  9 @implementation ViewController
 10 
 11 - (void)viewDidLoad
 12 {
 13     [super viewDidLoad];
 14     
 15     m_counter = 0;
 16     
 17     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 18  
 19     //这里需要说明一下  button.buttonType 是  readonly  不允许赋值!!!!
 20     
 21     
 22     
 23     
 24     button.frame =  CGRectMake(20, 40, WIDTH - 40, 40);
 25     
 26     
 27     //设置button是否可以被点击  - default is YES.
 28     button.enabled = YES;
 29     
 30     //设置button是否可以和用户之间交互  - default is YES.
 31     button.userInteractionEnabled = YES;
 32     
 33     
 34     //正常状态 -- 设置button的title
 35     [button setTitle:@"你确定要点击,别后悔!!!" forState:UIControlStateNormal];
 36     //高亮状态
 37     [button setTitle:@"你确定要点击,别后悔!!!" forState:UIControlStateHighlighted];
 38     //不可点击状态
 39     [button setTitle:@"你确定要点击,别后悔!!!" forState:UIControlStateDisabled];
 40     
 41     
 42     
 43     
 44     button.backgroundColor = [UIColor redColor];
 45     
 46     //UIControlStateNormal ---  UIControlStateHighlighted  ----  UIControlStateDisabled
 47     //不同状态下都可设置
 48     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 49     
 50     
 51     //UIControlStateNormal ---  UIControlStateHighlighted  ----  UIControlStateDisabled
 52     //不同状态下都可设置-----注:设置了背景图,那么背景颜色将不会生效!!!
 53     [button setBackgroundImage:[UIImage imageNamed:@"background"] forState:UIControlStateNormal];
 54     
 55     
 56     /*
 57      
 58      上面设设置背景图,现在是直接把button做成一张图来显示!!! 如果为button设置标题就不会显示
 59     //UIControlStateNormal ---  UIControlStateHighlighted  ----  UIControlStateDisabled
 60     //不同状态下都可设置
 61     [button setImage:[UIImage imageNamed:@"background"] forState:UIControlStateNormal];
 62      
 63     */
 64     
 65     
 66     
 67     //设置button在点击的时候时候会高亮一下儿 ---- default is NO
 68     [button setShowsTouchWhenHighlighted:YES];
 69     
 70     
 71     //设置按钮内部图片间距和标题间距
 72     UIEdgeInsets insets; // 设置按钮内部图片间距
 73     insets.top = insets.bottom = insets.right = insets.left = 10;
 74     button.contentEdgeInsets = insets;
 75     button.titleEdgeInsets = insets; // 标题间距
 76     
 77     
 78     //添加button点击事件
 79     [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
 80     /*
 81     //移除button事件----基本上不用!!!!
 82     [button removeTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
 83     */
 84     [self.view addSubview:button];
 85     
 86     
 87     
 88     //下面说一下button不常用的属性
 89     
 90     //怎么实现让button中的文字左对齐  或者右对齐
 91     button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
 92     //设置button的标题离边界的距离
 93     button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
 94     
 95     
 96     
 97     /*
 98      
 99      另外在说一点其他的工作上遇到的问题, 当启动一个定时器让button的标题不断改变的时候.(比如说在获取短信验证码的时候,系统会在点击一次后出现60s的倒计时每隔1s 改变一次button的标题)
100      */
101     
102     
103     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
104     button1.frame = CGRectMake(30, 100, WIDTH - 60, 40);
105     button1.tag = 1000;
106     button1.backgroundColor = [UIColor redColor];
107     [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
108     [button1 setTitle:@"点击开始倒计时" forState:UIControlStateNormal];
109     [button1 addTarget:self action:@selector(button1Click:) forControlEvents:UIControlEventTouchUpInside];
110     [self.view addSubview:button1];
111 }
112 -(void)button1Click:(UIButton *)sender
113 {
114      [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
115 }
116 
117 -(void)timerFired:(NSTimer *)sender
118 {
119     UIButton *button = [self.view viewWithTag:1000];
120     if (++m_counter > 60)
121     {
122         [sender invalidate];
123         [button setTitle:@"倒计时结束,点击重新开始" forState:UIControlStateNormal];
124         m_counter = 0;
125         button.enabled = YES;
126         return;
127     }
128     button.enabled = NO;
129     [button setTitle:[NSString stringWithFormat:@"%ld",60 - m_counter] forState:UIControlStateNormal];
130 }
131 
132 
133 -(void)buttonClick : (UIButton * )sender
134 {
135     NSLog(@"叫你不要点还要点,算你有种!!!!!");
136 }