• 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) 上有介绍,有兴趣的可以看看.
  • 相关阅读:
    ASP.NET API盘点
    C# POST与Get数据
    洛谷 P3373 【模板】线段树 2
    洛谷 P1972 [SDOI2009]HH的项链
    洛谷 P1113 杂务(vector)
    POJ 3249 Test for Job
    POJ 1734 Sightseeing trip(Floyd)
    洛谷 P1202 [USACO1.1]黑色星期五Friday the Thirteenth
    洛谷 P1484 种树
    洛谷 P1801 黑匣子_NOI导刊2010提高(06)
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6400566.html
Copyright © 2020-2023  润新知