IOS——模仿Android界面实现单选框UICheckBoxButton

首先要吐槽一下,IOS好好的Switch不用,非得要模仿Android搞一个单选框什么的最讨厌了啊啊啊啊啊啊啊啊~!!!!!

好吧,现在开始,实现自定义的模仿Android单选框的类命名为UICheckBoxButton,继承于UIControl

该UICheckbox功能:

1. 自定义选项的名字

2. 自定义选项框的大小(定义高度,因为选项框为正方形)

3. 自定义选项框的长度

4. 监听选项框的点击事件


上代码:

1. UICheckBoxButton.h

#import 

@interface UICheckBoxButton : UIControl{
    NSString *checkedImage;
    NSString *uncheckedImage;
    UIImageView *backgroundImage;
    UILabel  *titleLabel;
    BOOL     isChecked;
    id       delegate;
}

@property (retain, nonatomic) UIImageView *backgroundImage;
@property (retain, nonatomic) UILabel *titleLabel;
@property (retain, nonatomic) id  delegate;


- (id)initWithContent:(NSString *)titleName;
- (void)setOffsetFrame:(CGRect)frame;
- (BOOL)getChekced;
@end


2. UICheckBoxButton.m

#import "UICheckBoxButton.h"

@implementation UICheckBoxButton
@synthesize backgroundImage;
@synthesize titleLabel;
@synthesize delegate;

- (id)initWithContent:(NSString *)titleName {
    self = [super init];
    [self setBackgroundColor:[UIColor clearColor]];
    
    [self initImages];
    
    titleLabel = [[UILabel alloc] init];
    [titleLabel setText:titleName];
    [titleLabel setFont:[UIFont systemFontOfSize:14]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    
    backgroundImage = [[UIImageView alloc] init];
    isChecked = NO;
    [backgroundImage setImage:[UIImage imageNamed:uncheckedImage]];
    [self addSubview:backgroundImage];
    [self addSubview:titleLabel];
    [self addTarget:self action:@selector(clickEvent) forControlEvents:UIControlEventTouchUpInside];
    return self;
}

- (void)initImages {
    checkedImage = @"checkedIcon.png";
    uncheckedImage = @"uncheckedIcon.png";
}

- (void)setOffsetFrame:(CGRect)frame {
    CGSize frameSize;
    frameSize.width = frame.size.width;
    frameSize.height = frame.size.height;
    [super setFrame:CGRectMake(frame.origin.x, frame.origin.y, frameSize.width, frameSize.height)];
    
    CGRect iconFrame;
    iconFrame.size = CGSizeMake(frameSize.height,frameSize.height);
    iconFrame.origin.x = 0;
    iconFrame.origin.y = 0;
    [backgroundImage setFrame:iconFrame];
    
    CGRect lbFrame;
    lbFrame.size = CGSizeMake(frameSize.width, frameSize.height);
    lbFrame.origin.x = frame.size.height+5;
    lbFrame.origin.y = 0;
    [titleLabel setFrame:lbFrame];
}

- (void)clickEvent {
    if (isChecked) {
        isChecked=NO;
        [backgroundImage setImage:[UIImage imageNamed:uncheckedImage]];
    } else {
        isChecked=YES;
        [backgroundImage setImage:[UIImage imageNamed:checkedImage]];
    }
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

-(BOOL)getChekced{
    return isChecked;
}

@end

注意,image有两张,一张是选中的图片,另一张是没选中的图片。

这里贴上我自制的图片



3. 使用UICheckBoxButton

首先,

#import "UICheckBoxButton.h"

然后实现代码:

    dirveCheckbox=[[UICheckBoxButton alloc]initWithContent:@"Test"];//初始化checkbox的名称

    [dirveCheckbox setOffsetFrame:CGRectMake(10,230,120,28)];//初始化checkbox的位置,其中120为长度,28为高度

    [dirveCheckbox addTarget:self action:@selector(CheckBoxClicked:) forControlEvents:UIControlEventValueChanged];//给予checkbox点击事件监听


checkbox点击事件的监听方法:

-(void)CheckBoxClicked:(UICheckBoxButton *)checkbox{

    NSLog(@"click check box");

}



呼呼,完成!

你可能感兴趣的:(IOS)