zoukankan      html  css  js  c++  java
  • iOS开篇——UI之UIButton

    创建Button

        //创建一个button
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        //枚举类型
        /*
         typedef NS_ENUM(NSInteger, UIButtonType) {
         UIButtonTypeCustom = 0,                         // no button type
         UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button
         
         UIButtonTypeDetailDisclosure,
         UIButtonTypeInfoLight,
         UIButtonTypeInfoDark,
         UIButtonTypeContactAdd,
         
         UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
         };
         */

    设置button位置

    //设置button位置
        button.frame = CGRectMake(100, 100, 120, 160);

    设置button的title和状态

        [button setTitle:@"点击" forState:UIControlStateNormal];
    //正常状态下的Title为“点击”

    设置button背景色

        //设置button的title
        button.backgroundColor = [UIColor redColor];
        /*
         typedef NS_OPTIONS(NSUInteger, UIControlState) {
         UIControlStateNormal       = 0,  平时状态
         UIControlStateHighlighted  = 1 << 0,     高亮状态              // used when UIControl isHighlighted is set
         UIControlStateDisabled     = 1 << 1,   不允许点击状态
         UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
         UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
         UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
         UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
         };
         */

    以上为Button的各种状态

    设置Title颜色

        // 设置title颜色 平时状态
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    设置高亮下

        //设置button高亮标题
        [button setTitle:@"点了一下" forState:UIControlStateHighlighted];
        //设置button高亮状态文字颜色
        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        
        //设置button高亮状态下的背景图片
        [button setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateHighlighted];

    设置禁止点击

        //设置button禁止点击;
        button.userInteractionEnabled = YES;

    设置点击事件

        [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //枚举各种点击方式
        /*
         typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
         UIControlEventTouchDown                点下去                          = 1 <<  0,      // on all touch downs
         UIControlEventTouchDownRepeat              快速点击                    = 1 <<  1,      // on multiple touchdowns (tap count > 1)
         UIControlEventTouchDragInside       点击滑动 button内                            = 1 <<  2,
         UIControlEventTouchDragOutside              点击滑动 button外                    = 1 <<  3,
         UIControlEventTouchDragEnter                                    = 1 <<  4,
         UIControlEventTouchDragExit                                     = 1 <<  5,
         UIControlEventTouchUpInside                                     = 1 <<  6,
         UIControlEventTouchUpOutside                                    = 1 <<  7,
         UIControlEventTouchCancel                                       = 1 <<  8,
         
         UIControlEventValueChanged                                      = 1 << 12,     // sliders, etc.
         UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13,     // semantic action: for buttons, etc.
         
         UIControlEventEditingDidBegin                                   = 1 << 16,     // UITextField
         UIControlEventEditingChanged                                    = 1 << 17,
         UIControlEventEditingDidEnd                                     = 1 << 18,
         UIControlEventEditingDidEndOnExit                               = 1 << 19,     // 'return key' ending editing
         
         UIControlEventAllTouchEvents                                    = 0x00000FFF,  // for touch events
         UIControlEventAllEditingEvents                                  = 0x000F0000,  // for UITextField
         UIControlEventApplicationReserved                               = 0x0F000000,  // range available for application use
         UIControlEventSystemReserved                                    = 0xF0000000,  // range reserved for internal framework use
         UIControlEventAllEvents                                         = 0xFFFFFFFF
         };
    
         */

    Label碰撞反弹事件代码

      1 #import "ViewController.h"
      2 
      3 @interface ViewController (){
      4     //x坐标
      5     float x;
      6     //y坐标
      7     float y;
      8     //x位移量
      9     float vX;
     10     //y位移量
     11     float vY;
     12     //点击计数器
     13     int times;
     14     
     15     //计时器
     16     NSTimer * timer;
     17     //标签
     18     UILabel * label;
     19 //    UIImageView * imgView;
     20 }
     21 
     22 @end
     23 
     24 @implementation ViewController
     25 
     26 - (void)viewDidLoad {
     27     [super viewDidLoad];
     28     //创建按钮  实现点击方法
     29     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
     30     button.frame = CGRectMake((self.view.frame.size.width-150)/2, (self.view.frame.size.height-50)/2, 150, 50);
     31     [button setTitle:@"点击开始/暂停" forState:UIControlStateNormal];
     32     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     33     [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
     34     button.backgroundColor = [UIColor groupTableViewBackgroundColor];
     35     
     36 //    imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
     37 //    imgView.frame = CGRectMake(0, 0, 100, 150);
     38 //    [self.view addSubview:imgView];
     39     
     40     //设置x,y。times归0 设置位移量
     41     x = 0;
     42     y = 0;
     43     times = 0;
     44     vX = 0.01;
     45     vY = 0.01;
     46     
     47     //创建标签
     48     label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 200, 35)];
     49     label.text = @"这是一个很神奇的标签";
     50     label.adjustsFontSizeToFitWidth = YES;
     51     label.textAlignment = NSTextAlignmentCenter;
     52     
     53     
     54     
     55     [button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
     56     [self.view addSubview:label];
     57     [self.view addSubview:button];
     58     // Do any additional setup after loading the view, typically from a nib.
     59 }
     60 
     61 - (void)onClick{
     62     //点击后 点击计数器加1
     63     times++;
     64     
     65     //计数器为奇数 开始计时器 偶数暂停
     66     if (times%2==1) {
     67         [self startTimer];
     68     }else if (times%2 == 0) {
     69         
     70         [self stopTimer];
     71     }
     72 }
     73 - (void)startTimer{
     74     //设置计时器 循环调用move方法
     75     timer = [NSTimer scheduledTimerWithTimeInterval:0.0001 target:self selector:@selector(move) userInfo:nil repeats:YES];
     76 }
     77 - (void)stopTimer{
     78     [timer setFireDate:[NSDate distantFuture]];
     79 }
     80 
     81 -(void)move{
     82     //若x超边界  则vX取反  x,y改变位置
     83     if (x>self.view.frame.size.width-200||x<0) {
     84         vX = 0-vX;
     85         x = x+vX;
     86         y = y+vY;
     87     }else if (y >self.view.frame.size.height-35||y<0) {
     88         //若y超边界 则vY取反   x,y改变位置
     89         vY = 0-vY;
     90         x = x+vX;
     91         y = y+vY;
     92     }else{
     93         //x,y改变位置
     94         x = x+vX;
     95         y = y+vY;
     96     }
     97     
     98     //重置label位置
     99     [label setFrame:CGRectMake(x, y, 200, 35)];
    100     
    101 }
    102 
    103 - (void)didReceiveMemoryWarning {
    104     [super didReceiveMemoryWarning];
    105     // Dispose of any resources that can be recreated.
    106 }
    107 
    108 @end
    ViewController.m
  • 相关阅读:
    设计模式二(建造者、原型、桥接)
    MSSQL根据表名动态分页的存储过程以及C#.net调用使用
    查询身份证号码信息(C#.NET)
    初试三层+抽象工厂代码生成器
    NET多线程与异步编程
    【SQL.SERVER.DMVS.实战】学习笔记
    【SQL.SERVER.DMVS.实战】学习笔记(二)
    SQL Server 2005数据文件数据的存储
    ASP.NET第九天加强课程
    ASP.NET第四天加强课程
  • 原文地址:https://www.cnblogs.com/gwkiOS/p/4973360.html
Copyright © 2011-2022 走看看