zoukankan      html  css  js  c++  java
  • iOS

    • 看图

    • 代码

    
    //
    //  ViewController.m
    //  test
    //
    //  Created by 裴波波 on 2017/2/15.
    //  Copyright © 2017年 裴波波. All rights reserved.
    //
    
    #import "ViewController.h"
    #define KScreen_Bounds [UIScreen mainScreen].bounds
    #define KScreen_Size [UIScreen mainScreen].bounds.size
    #define KScreen_Width [UIScreen mainScreen].bounds.size.width
    #define KScreen_Height [UIScreen mainScreen].bounds.size.height
    @interface ViewController ()
    //临时lineView 与 临时 btn
    @property (strong, nonatomic) UIView * lineViewTemp;
    @property (strong, nonatomic) UIButton * btnTemp;
    //lineView数组
    @property (strong, nonatomic) NSMutableArray * arrMLineView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        self.arrMLineView = @[].mutableCopy;
        [self setLablesWithArrOfTitle:@[@"one",@"two",@"three"] andLeftDistance:0 andItWidth:KScreen_Width/3 andItHeight:35 andYcoordinate:64 andDestiView:self.view];
    }
    
    -(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItWidth:(CGFloat)width andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{
        
        CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
        CGFloat margin = (screenWidth - 2*lDistance - width*arrTitles.count) / (arrTitles.count - 1);
        UIButton * tempLabel = [UIButton new];
        
        for (int i = 0; i< arrTitles.count; i++) {
            
            if (i == 0) {
                
                UIButton * lbl = [[UIButton alloc] initWithFrame:CGRectMake(lDistance, y, width, height)];
                lbl.tag = i;
                [lbl setBackgroundColor:[UIColor orangeColor]];
                [lbl setTitle:arrTitles[0] forState:UIControlStateNormal];
                [destiView addSubview:lbl];
                //line
                UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, height - 5+64, width, 5)];
                [self.arrMLineView addObject:lineView];
                //设置默认第一个选中
                lineView.backgroundColor = [UIColor redColor];
                [lbl setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                _btnTemp = lbl;
                _lineViewTemp = lineView;
                [destiView addSubview:lineView];
                lineView.hidden = NO;
                tempLabel = lbl;
                //绑定点击事件
                [lbl addTarget:self action:@selector(clickBtnOne:) forControlEvents:UIControlEventTouchUpInside];
                continue;
            }
            UIButton * lbl = [[UIButton alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), y, width, height)];
            lbl.tag = i;
            [lbl setBackgroundColor:[UIColor orangeColor]];
            [lbl setTitle:arrTitles[i] forState:UIControlStateNormal];
            [destiView addSubview:lbl];
            UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), height - 5+64, width, 5)];
            [self.arrMLineView addObject:lineView];
            lineView.hidden = YES;
            lineView.backgroundColor = [UIColor redColor];
            [destiView addSubview:lineView];
            //绑定点击事件
            if (i == 1) {
                [lbl addTarget:self action:@selector(clickBtnOne:) forControlEvents:UIControlEventTouchUpInside];
            }else if (i == 2){
                [lbl addTarget:self action:@selector(clickBtnOne:) forControlEvents:UIControlEventTouchUpInside];
            }
            tempLabel = lbl;
        }
    }
    
    -(void)testHiddenWithBtn:(UIButton *)sender{
    
        if (_btnTemp != sender) {
            [_btnTemp setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            _lineViewTemp.hidden = YES;
            
            _btnTemp = sender;
            [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            _lineViewTemp = self.arrMLineView[sender.tag];
            _lineViewTemp.hidden = NO;
        }
    }
    
    -(void)clickBtnOne:(UIButton *)sender{
        
        [self testHiddenWithBtn:sender];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    
    
    
    • btn 绑定了tag,减少了许多重复代码.
    • btn不同的点击事件只需要在方法-(void)clickBtnOne:(UIButton *)sender中判断tag值即可
    • 这个方法-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItWidth:(CGFloat)width andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView在我的这个博客(http://blog.csdn.net/alex_birdlion/article/details/52932562) 上有介绍,有兴趣的可以看看.
  • 相关阅读:
    移动平台开发第七周学习总结
    团队作业(2)项目选题
    人工智能汇总---政策-应用--技术
    机器人(人工智能python) arduino编程 scratch2.0 乐高ev3编程 教育政策及考试
    Scratch工具下载及学习视频等资料
    2019人工智能科普--汇总
    2018人工智能应用例子_汇总贴
    北大人工智能前沿讲座--第二讲 嵌入式人工智能
    《焦点访谈》 20180405 美举“大棒”我“亮剑”
    window10下安装python3.6.x 及环境变量的配置
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6400566.html
Copyright © 2011-2022 走看看