天天看點

IOS 單選框

這個單選框代碼,隻需通過參數設定就可以控制顯示的效果。

IOS 單選框
IOS 單選框

1、設定單個單選的樣子。

@interface RadioView : UIView

@property (assign,nonatomic)BOOL isSelect;

-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)title;

@end

@interface RadioView()

@property (strong,nonatomic)UIImageView *imageView;

@end

@implementation RadioView

-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)title

{

    self = [superinitWithFrame:frame];

    if (self) {

        [self setup:title];

    }

    return self;

}

-(void)setup:(NSString *)title

{

    UIImage *image = [UIImageimageNamed:@"pref_checkbox_normal"];

    UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(10,10, 30, 30)];

    [imageView setImage:image];

    [imageView setContentMode:UIViewContentModeScaleAspectFit];

    [self addSubview:imageView];

    self.imageView = imageView;

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) +10, 10, 100,30)];

    [label setText:title];

    [self addSubview:label];

}

-(void)setIsSelect:(BOOL)isSelect

{

    _isSelect = isSelect;

    UIImage *image = [UIImageimageNamed:@"pref_checkbox_normal"];

    if (isSelect) {

        image = [UIImageimageNamed:@"pref_checkbox_checked"];

    }

    [self.imageViewsetImage:image];

}

@end

2、單選的顯示控制

@interface RView : UIView

@property (strong,nonatomic)NSArray *radioTitleArray;

@property (assign,nonatomic)NSInteger singleLineNumber;

-(void)setupRadioControl;

@end

@interface RView()

@property (strong,nonatomic)NSMutableArray *radioArrayM;

@end

@implementation RView

-(void)setupRadioControl

{

    if(self.radioArrayM){

        [self.radioArrayMremoveAllObjects];

    }

    else{

        self.radioArrayM = [NSMutableArrayarray];

    }

    CGSize size = [UIScreenmainScreen].bounds.size;

    CGFloat w = size.width /self.singleLineNumber;

    CGFloat h = 40;

    CGFloat x = 0;

    CGFloat y = 0;

    NSInteger num = 0;

    for (NSInteger i =0; i < self.radioTitleArray.count; i++) {

        NSInteger line = i / self.singleLineNumber;

        if (num >= self.singleLineNumber) {

            num = 0;

        }

        x = num * w;

        y = line * h;

        RadioView *rv = [[RadioViewalloc]initWithFrame:CGRectMake(x, y, w, h)title:self.radioTitleArray[i]];

        MyTapGestureRecognizer *mt = [[MyTapGestureRecognizeralloc]initWithTarget:selfaction:@selector(click:)];

        mt.rv = rv;

        [rv addGestureRecognizer:mt];

        [self addSubview:rv];

        [self.radioArrayMaddObject:rv];

        num++;

        if (i == 0) {

            rv.isSelect = YES;

        }

    }

}

-(void)click:(MyTapGestureRecognizer *)mt

{

    for (RadioView *rvin self.radioArrayM) {

        if ([rv isEqual:mt.rv]) {

            rv.isSelect = YES;

        }

        else{

            rv.isSelect = NO;

        }

    }

}

@end

3、自定義的MyTapGestureRecognizer

@class RadioView;

@interface MyTapGestureRecognizer : UITapGestureRecognizer

@property (strong,nonatomic) RadioView *rv;

@end

4、最終在UIViewController中的使用

-(void)setup

{

    RView *r = [[RViewalloc]initWithFrame:self.view.frame];

    r.radioTitleArray = @[@"XX1單",@"XX1單",@"XX1單",@"XX1單",@"XX1單",@"XX1單"];

    r.singleLineNumber = 1; //通過設定數量來控制顯示

    [self.viewaddSubview:r];

    [r setupRadioControl]; //設定完成後執行這個方法

}