ios自定义checkbox
//Author:smilelance
//From:http://blog.csdn.net/smilelance
#import <UIKit/UIKit.h>
@interface PDECheckBox : UIButton
{
BOOL isChecked;
}
- (id)initWithCenter:(CGPoint)center;
- (BOOL)isChecked;
- (void)setCheckState:(BOOL)checked;
- (void)switchCheckState;
@end
#import "PDECheckBox.h"
#define IMAGE_CHECKED @"checkbox_gray_sel.png"
#define IMAGE_UNCHECKED @"checkbox_gray.png"
@implementation PDECheckBox
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithCenter:(CGPoint)center
{
// UIImage *imgChecked = [UIImage imageNamed:IMAGE_CHECKED];
UIImage *imgUnChecked = [UIImageimageNamed:IMAGE_UNCHECKED];
CGSize size = imgUnChecked.size;
CGRect frame = CGRectMake(center.x-size.width/2, center.y-size.height/2,
size.width, size.height);
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[selfsetImage:imgUnChecked forState:UIControlStateNormal];
isChecked = NO;
[selfaddTarget:selfaction:@selector(switchCheckState)forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (BOOL)isChecked
{
returnisChecked;
}
- (void)switchCheckState
{
[selfsetCheckState:!isChecked];
}
- (void)setCheckState:(BOOL)checked
{
if (checked != isChecked) {
isChecked = checked;
if (isChecked) {
[selfsetImage:[UIImageimageNamed:IMAGE_CHECKED]forState:UIControlStateNormal];
}else {
[selfsetImage:[UIImageimageNamed:IMAGE_UNCHECKED]forState:UIControlStateNormal];
}
}
}
@end