这个单选框代码,只需通过参数设置就可以控制显示的效果。
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.singleLineNumber = 1; //通过设置数量来控制显示
[self.viewaddSubview:r];
[r setupRadioControl]; //设置完成后执行这个方法
}