單選以及多選的實現 iOS

話不多說,首先看效果圖


單選.gif

多選.gif

1、定義UIView的擴展類

UIView+DBExtension.h

import <UIKit/UIKit.h>

@interface UIView (DBExtension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGSize size;
@property (nonatomic, assign) CGPoint origin;
@end

UIView+DBExtension.m

@implementation UIView (DBExtension)

// set

  • (void)setX:(CGFloat)x {

    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
    }

  • (void)setY:(CGFloat)y {

    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
    }

  • (void)setCenterX:(CGFloat)centerX {

    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
    }

  • (void)setCenterY:(CGFloat)centerY {

    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
    }

  • (void)setWidth:(CGFloat)width {

    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
    }

  • (void)setHeight:(CGFloat)height {

    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
    }

  • (void)setSize:(CGSize)size {

    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
    }

  • (void)setOrigin:(CGPoint)origin {

    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
    }

// get

  • (CGFloat)x {
    return self.frame.origin.x;
    }

  • (CGFloat)y {
    return self.frame.origin.y;
    }

  • (CGFloat)centerX {
    return self.center.x;
    }

  • (CGFloat)centerY {
    return self.center.y;
    }

  • (CGFloat)width {
    return self.frame.size.width;
    }

  • (CGFloat)height {
    return self.frame.size.height;
    }

  • (CGSize)size {
    return self.frame.size;
    }

  • (CGPoint)origin {
    return self.frame.origin;
    }

@end

其次
首先實現多選功能

// 設置多選視圖

  • (void)setupMultiselectView {

    CGFloat UI_View_Width = [UIScreen mainScreen].bounds.size.width;
    CGFloat marginX = 15;
    CGFloat top = 19;
    CGFloat btnH = 35;
    CGFloat marginH = 40;
    CGFloat height = 130;
    CGFloat width = (UI_View_Width - marginX * 4) / 3;

    // 按鈕背景
    UIView *btnsBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, UI_View_Width, height)];
    btnsBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:btnsBgView];

    // 循環創建按鈕
    NSInteger maxCol = 3;
    for (NSInteger i = 0; i < 5; i++) {

      UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
      btn.backgroundColor = 自己設置的顏色;
      btn.layer.cornerRadius = 3.0; // 按鈕的邊框弧度
      btn.clipsToBounds = YES;
      btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
      [btn setTitleColor:[UIColor colorWithRed:(102)/255.0 green:(102)/255.0 blue:(102)/255.0 alpha:1.0] forState:UIControlStateNormal];
      [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
      [btn addTarget:self action:@selector(chooseMark:) forControlEvents:UIControlEventTouchUpInside];
      NSInteger col = i % maxCol; //列
      btn.x  = marginX + col * (width + marginX);
      NSInteger row = i / maxCol; //行
      btn.y = top + row * (btnH + marginX);
      btn.width = width;
      btn.height = btnH;
      [btn setTitle: 按鈕標題數組 [i] forState:UIControlStateNormal];
      [btnsBgView addSubview:btn];
    

    }

    // 確定按鈕
    UIButton *surebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [surebtn setTitle:@"確定" forState:UIControlStateNormal];
    surebtn.frame = CGRectMake(marginX * 2, CGRectGetMaxY(btnsBgView.frame) + marginH, UI_View_Width - marginX * 4, 40);
    surebtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [surebtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    surebtn.backgroundColor = [UIColor orangeColor];
    surebtn.layer.cornerRadius = 3.0;
    surebtn.clipsToBounds = YES;
    [self.view addSubview:surebtn];
    }

/**

  • 按鈕多選處理
    */
  • (void)chooseMark:(UIButton *)btn {

    btn.selected = !btn.selected;

    if (btn.isSelected) {
    btn.backgroundColor = 自己設置的顏色;
    [self.selectedMarkArray addObject:self.markDict[btn.titleLabel.text]];
    [self.selectedMarkStrArray addObject:btn.titleLabel.text];
    } else {
    btn.backgroundColor = 自己設置的顏色;
    [self.selectedMarkArray removeObject:self.markDict[btn.titleLabel.text]];
    [self.selectedMarkStrArray removeObject:btn.titleLabel.text];
    }
    }

最后實現單選按鈕

// 設置單選視圖

  • (void)setupRadioBtnView {

    CGFloat UI_View_Width = [UIScreen mainScreen].bounds.size.width;
    CGFloat marginX = 15;
    CGFloat top = 100;
    CGFloat btnH = 30;
    CGFloat width = (250 - marginX * 4) / 3;

    // 按鈕背景
    UIView *btnsBgView = [[UIView alloc] initWithFrame:CGRectMake((UI_View_Width - 250) * 0.5, 50, 250, self.markArray.count *btnH + self.markArray.count/3 * (btnH + marginX))];
    btnsBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:btnsBgView];

    // 循環創建按鈕
    NSInteger maxCol = 3;
    for (NSInteger i = 0; i < self.markArray.count; i++) {

      UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
      btn.backgroundColor = [UIColor lightGrayColor];
      btn.layer.cornerRadius = 3.0; // 按鈕的邊框弧度
      btn.clipsToBounds = YES;
      btn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
      [btn setTitleColor:[UIColor colorWithRed:(102)/255.0 green:(102)/255.0 blue:(102)/255.0 alpha:1.0] forState:UIControlStateNormal];
      [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
      [btn addTarget:self action:@selector(chooseMark:) forControlEvents:UIControlEventTouchUpInside];
      NSInteger col = i % maxCol; //列
      btn.x = marginX + col * (width + marginX);
      NSInteger row = i / maxCol; //行
      btn.y = top + row * (btnH + marginX);
      btn.width = width;
      btn.height = btnH;
      [btn setTitle:self.markArray[i] forState:UIControlStateNormal];
      [btnsBgView addSubview:btn];
      btn.tag = i;
      [self.btnArray addObject:btn];
    

    }
    //獲取最后一個按鈕的偏移量
    UIButton *lastBtn = _btnArray[_btnArray.count - 1];
    NSLog(@"%f",lastBtn.y);
    // 創建完btn后再判斷是否能選擇(之前是已經選取過的)
    // 假數據:之前已經上傳16時,則回顯16
    // for (UIButton *btn in btnsBgView.subviews) {
    // if ([@"16" isEqualToString:btn.titleLabel.text]) {
    // btn.selected = YES;
    // btn.backgroundColor = ZLSelectedColor;
    // break;
    // }
    // }
    }

/**

  • 數字按鈕單選處理
    */
  • (void)chooseMark:(UIButton *)sender {
    NSLog(@"點擊了%@", sender.titleLabel.text);

    self.selectedBtn = sender;

    sender.selected = !sender.selected;

    for (NSInteger j = 0; j < [self.btnArray count]; j++) {
    UIButton *btn = button數組 [j] ;
    if (sender.tag == j) {
    btn.selected = sender.selected;
    } else {
    btn.selected = NO;
    }

      btn.backgroundColor = [UIColor lightGrayColor];
    

    }

    // 根據tag值去判斷
    UIButton *btn = self.btnArray[sender.tag];
    if (btn.selected) {

      btn.backgroundColor = [UIColor blueColor];
    

    } else {

      btn.backgroundColor = [UIColor lightGrayColor];
    

    }
    }

希望對你有所幫助!

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容