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;

}


/**

 *  设置控件

 *

 *  @param title 单选框文字

 */

-(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];

}


/**

 *  设置选中和取消选中的效果

 *

 *  @param isSelect BOOL

 */

-(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.singleLineNumber1; //通过设置数量来控制显示

    

    [self.viewaddSubview:r];

    

    [r setupRadioControl]; //设置完成后执行这个方法

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值