iOS - 菜單欄menu選擇控件


  • 看圖

  • 代碼


//
//  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) 上有介紹,有興趣的可以看看.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM