- 论坛徽章:
- 0
|
实现与android平台上相同的RadioGroup控件
//使用是只需要实现RadioGroup代理即可完成监听事件
Objective-C- //RadioButton实现
-
- #import <UIKit/UIKit.h>
- #import <QuartzCore/QuartzCore.h>
- #define RadioButton_WHeight 25
- #define RadioButton_Space 5
- typedef enum {
- CCRadioButtonAligmentLeft, //图片在左方
- CCRadioButtonAligmentRight
- }CCRadioButtonAligment;
-
- @protocol CCRadioButtonDelegate;
- @interface CCRadioButton : UIControl
- {
- @private
- CCRadioButtonAligment _aligment;
- CGSize _textSize;
- //使用layer减少内存消耗
- CALayer *_imageLayer;
- CATextLayer *_labelLayer;
- }
- @property (nonatomic, strong) UIImage *image; //图片
- @property (nonatomic, strong) UIColor *textColor; //字体颜色
- @property (assign, nonatomic) id<CCRadioButtonDelegate> delegate;
- @property (nonatomic, getter = isChecked, setter = setCheck:) BOOL check;
- @property (nonatomic, strong) NSString *text;
-
- - (id)initWithFrame:(CGRect)frame text:(NSString *)text aligment:(CCRadioButtonAligment)aligment isChecked:(BOOL)check;
- - (void)setCheck:(BOOL)check;
- @end
-
- @protocol CCRadioButtonDelegate <NSObject>
-
- - (void)onCheckedChanged:(CCRadioButton *)button;
-
- @end
-
- #import "CCRadioButton.h"
- #import "CCUITool.h"
-
- @implementation CCRadioButton
-
- - (id)initWithFrame:(CGRect)frame text:(NSString *)text aligment:(CCRadioButtonAligment)aligment isChecked:(BOOL)check
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor clearColor];
- _check = check;
- _aligment = aligment;
- _text = text;
- /* 添加target */
- [self addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventTouchUpInside];
- //更具字的size 计算本控件的frame
- _textSize = [_text sizeWithFont:[CCUITool getDefaultFont:14] constrainedToSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)];
- self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, RadioButton_WHeight + _textSize.width + RadioButton_Space, RadioButton_WHeight > _textSize.height ? RadioButton_WHeight : _textSize.height);
- }
- return self;
- }
-
- - (void)drawRect:(CGRect)rect
- {
- if (!_imageLayer && !_labelLayer) {
- _imageLayer = [CALayer layer];
- //计算_imageLayer 与_labelLayer 的frame
- _imageLayer.frame = _aligment == CCRadioButtonAligmentLeft ? CGRectMake(_textSize.width + 5, _textSize.height > RadioButton_WHeight ? (_textSize.height - RadioButton_WHeight)/2.0 : 0, RadioButton_WHeight, RadioButton_WHeight) : CGRectMake(0, _textSize.height > RadioButton_WHeight ? (_textSize.height - RadioButton_WHeight)/2.0 : 0, RadioButton_WHeight, RadioButton_WHeight);
- [self.layer addSublayer:_imageLayer];
-
- _labelLayer = [CATextLayer layer];
- _labelLayer.string = _text;
- _labelLayer.fontSize = 14;
- _labelLayer.frame = _aligment == CCRadioButtonAligmentLeft ? CGRectMake(0, _textSize.height > RadioButton_WHeight ? 0 : (RadioButton_WHeight - _textSize.height) / 2.0, _textSize.width, _textSize.height > RadioButton_WHeight ? _textSize.height : RadioButton_WHeight) : CGRectMake(RadioButton_WHeight+RadioButton_Space, (self.frame.size.height - _textSize.height)/2.0, _textSize.width, _textSize.height);
- [self.layer addSublayer:_labelLayer];
- }
-
- / *重新切换图片,字体颜色(选中为红色)*/
- _image = [UIImage imageNamed:_check ? @"radiobutton_checked" : @"radiobutton_normal"];
- _textColor = _check ? [UIColor redColor] : [CCUITool getDefaultTextColor];
- _imageLayer.contents = (id)_image.CGImage;
- _labelLayer.foregroundColor = _textColor.CGColor;
- }
-
- - (void)valueChanged
- {
- if (!_check) {
- _check = !_check;
- if (_delegate && [_delegate respondsToSelector:@selector(onCheckedChanged:)]) {
- [_delegate onCheckedChanged:self];
- }
- }
- }
-
- - (void)setCheck:(BOOL)check
- {
- _check = check;
- [self setNeedsDisplay];
- }
-
- @end
-
- //RadioGroup实现
- #import <UIKit/UIKit.h>
- #import "CCRadioButton.h"
- typedef enum {
- CCRadioGroupOrientationHorizental, //RadioGroup水平放置
- CCRadioGroupOrientationVertical
- }CCRadioGroupOrientation;
-
- @protocol CCRadioGroupDelegate;
- @interface CCRadioGroup : UIView<CCRadioButtonDelegate/*实现RadioButton代理 */>
- {
- @private
- NSMutableArray *_buttonArray;
- CCRadioButton *_currentButton;
- }
- @property (assign, nonatomic) NSUInteger checkedIndex;
- @property (assign, nonatomic) id<CCRadioGroupDelegate> delegate;
-
- - (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)array orientation:(CCRadioGroupOrientation)orientation;
- - (void)checkIndex:(NSUInteger)index;
- - (CCRadioButton *)getCheckedRadioButton:(NSUInteger)checkId;
-
- @end
- #import "CCRadioGroup.h"
- #import "CCUITool.h"
- #import "CCRadioGroupDelegate.h"
-
- @implementation CCRadioGroup
-
- - (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)array orientation:(CCRadioGroupOrientation)orientation
- {
- self = [super initWithFrame:frame];
- if (self) {
- _buttonArray = [NSMutableArray array];
- CGFloat tempY = 0;
- for (int i = 0; i < array.count; i++) {
- CCRadioButton *radioButton = nil;
- switch (orientation) {
- case CCRadioGroupOrientationVertical:
- radioButton = [[CCRadioButton alloc] initWithFrame:CGRectMake(0, tempY, 0, 0) text:[array objectAtIndex:i] aligment:CCRadioButtonAligmentRight isChecked:NO];
- tempY += 10;
- break;
- case CCRadioGroupOrientationHorizental:
- radioButton = [[CCRadioButton alloc] initWithFrame:CGRectMake(i * self.frame.size.width/array.count, 0, 0, 0) text:[array objectAtIndex:i] aligment:CCRadioButtonAligmentRight isChecked:NO];
- break;
- }
- radioButton.tag = i;
- if (i == 0) {
- _currentButton = radioButton;
- }
- radioButton.delegate = self;
- [self addSubview:radioButton];
- [_buttonArray addObject:radioButton];
- }
- }
- return self;
- }
-
- - (void)checkIndex:(NSUInteger)index
- {
- _currentButton = [_buttonArray objectAtIndex:index];
- _currentButton.check = YES;
- _checkedIndex = index;
- }
-
- - (CCRadioButton *)getCheckedRadioButton:(NSUInteger)checkId
- {
- return (CCRadioButton *)[_buttonArray objectAtIndex:checkId];
- }
-
- #pragma mark - CCRadioButtonDelegate
- - (void)onCheckedChanged:(CCRadioButton *)button
- {
- [_currentButton setCheck:NO];
- _currentButton = button;
- [_currentButton setCheck:YES];
- _checkedIndex = button.tag;
- if (_delegate && [_delegate respondsToSelector:@selector(onCheckedChanged:index:)]) {
- [_delegate onCheckedChanged:self index:button.tag];
- }
- }
-
- @end
- //RadioGroup代理
- #import <Foundation/Foundation.h>
-
- @class CCRadioGroup;
- @protocol CCRadioGroupDelegate <NSObject>
-
- - (void)onCheckedChanged:(CCRadioGroup *)radioGroup index:(NSUInteger)checkId;
-
- @end
-
-
- //使用实例
- NSArray *textArray = @[@"一月", @"半年", @"一年"];
- CCRadioGroup *radioGroup = [[CCRadioGroup alloc] initWithFrame:CGRectMake(5, 2, SCREEN_WIDTH - 10, DEFAULT_HEIGHTLB) buttonTitle:textArray orientation:CCRadioGroupOrientationHorizental];
- radioGroup.delegate = self;
-
- //实现代理
- - (void)onCheckedChanged:(CCRadioGroup *)radioGroup index:(NSUInteger)checkId
- {
- NSLog(@"点击了 -- %@", [radioGroup getCheckedRadioButton:checkId].text);
- }
复制代码 |
|