免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1469 | 回复: 0
打印 上一主题 下一主题

[iOS] IOS自定义RadioGroup [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-10 15:24 |只看该作者 |倒序浏览
实现与android平台上相同的RadioGroup控件
//使用是只需要实现RadioGroup代理即可完成监听事件

Objective-C
  1. //RadioButton实现

  2. #import <UIKit/UIKit.h>
  3. #import <QuartzCore/QuartzCore.h>
  4. #define RadioButton_WHeight 25
  5. #define RadioButton_Space 5
  6. typedef enum {
  7.     CCRadioButtonAligmentLeft,  //图片在左方
  8.     CCRadioButtonAligmentRight
  9. }CCRadioButtonAligment;

  10. @protocol  CCRadioButtonDelegate;
  11. @interface CCRadioButton : UIControl
  12. {
  13. @private
  14.     CCRadioButtonAligment _aligment;
  15.     CGSize _textSize;
  16.     //使用layer减少内存消耗
  17.     CALayer *_imageLayer;
  18.     CATextLayer *_labelLayer;
  19. }
  20. @property (nonatomic, strong) UIImage *image;   //图片
  21. @property (nonatomic, strong) UIColor *textColor;  //字体颜色
  22. @property (assign, nonatomic) id<CCRadioButtonDelegate> delegate;
  23. @property (nonatomic, getter = isChecked, setter = setCheck:) BOOL check;
  24. @property (nonatomic, strong) NSString *text;

  25. - (id)initWithFrame:(CGRect)frame text:(NSString *)text aligment:(CCRadioButtonAligment)aligment isChecked:(BOOL)check;
  26. - (void)setCheck:(BOOL)check;
  27. @end

  28. @protocol CCRadioButtonDelegate <NSObject>

  29. - (void)onCheckedChanged:(CCRadioButton *)button;

  30. @end

  31. #import "CCRadioButton.h"
  32. #import "CCUITool.h"

  33. @implementation CCRadioButton

  34. - (id)initWithFrame:(CGRect)frame text:(NSString *)text aligment:(CCRadioButtonAligment)aligment isChecked:(BOOL)check
  35. {
  36.     self = [super initWithFrame:frame];
  37.     if (self) {
  38.         self.backgroundColor = [UIColor clearColor];
  39.         _check = check;
  40.         _aligment = aligment;
  41.         _text = text;
  42.         /* 添加target */
  43.         [self addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventTouchUpInside];
  44.          //更具字的size 计算本控件的frame
  45.         _textSize = [_text sizeWithFont:[CCUITool getDefaultFont:14] constrainedToSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT)];
  46.         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);
  47.     }
  48.     return self;
  49. }

  50. - (void)drawRect:(CGRect)rect
  51. {
  52.     if (!_imageLayer && !_labelLayer) {
  53.         _imageLayer = [CALayer layer];
  54.         //计算_imageLayer 与_labelLayer 的frame
  55.         _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);
  56.         [self.layer addSublayer:_imageLayer];
  57.          
  58.         _labelLayer = [CATextLayer layer];
  59.         _labelLayer.string = _text;
  60.         _labelLayer.fontSize = 14;
  61.         _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);
  62.         [self.layer addSublayer:_labelLayer];
  63.     }
  64.      
  65.     / *重新切换图片,字体颜色(选中为红色)*/
  66.     _image = [UIImage imageNamed:_check ? @"radiobutton_checked" : @"radiobutton_normal"];
  67.     _textColor = _check ? [UIColor redColor] : [CCUITool getDefaultTextColor];
  68.     _imageLayer.contents = (id)_image.CGImage;
  69.     _labelLayer.foregroundColor = _textColor.CGColor;
  70. }

  71. - (void)valueChanged
  72. {
  73.     if (!_check) {
  74.         _check = !_check;
  75.         if (_delegate && [_delegate respondsToSelector:@selector(onCheckedChanged:)]) {
  76.             [_delegate onCheckedChanged:self];
  77.         }
  78.     }
  79. }

  80. - (void)setCheck:(BOOL)check
  81. {
  82.     _check = check;
  83.     [self setNeedsDisplay];
  84. }

  85. @end

  86. //RadioGroup实现
  87. #import <UIKit/UIKit.h>
  88. #import "CCRadioButton.h"
  89. typedef enum {
  90.     CCRadioGroupOrientationHorizental,  //RadioGroup水平放置
  91.     CCRadioGroupOrientationVertical
  92. }CCRadioGroupOrientation;

  93. @protocol CCRadioGroupDelegate;
  94. @interface CCRadioGroup : UIView<CCRadioButtonDelegate/*实现RadioButton代理 */>
  95. {
  96. @private
  97.     NSMutableArray *_buttonArray;
  98.     CCRadioButton *_currentButton;
  99. }
  100. @property (assign, nonatomic) NSUInteger checkedIndex;
  101. @property (assign, nonatomic) id<CCRadioGroupDelegate> delegate;

  102. - (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)array orientation:(CCRadioGroupOrientation)orientation;
  103. - (void)checkIndex:(NSUInteger)index;
  104. - (CCRadioButton *)getCheckedRadioButton:(NSUInteger)checkId;

  105. @end
  106. #import "CCRadioGroup.h"
  107. #import "CCUITool.h"
  108. #import "CCRadioGroupDelegate.h"

  109. @implementation CCRadioGroup

  110. - (id)initWithFrame:(CGRect)frame buttonTitle:(NSArray *)array orientation:(CCRadioGroupOrientation)orientation
  111. {
  112.     self = [super initWithFrame:frame];
  113.     if (self) {
  114.         _buttonArray = [NSMutableArray array];
  115.         CGFloat tempY = 0;
  116.         for (int i = 0; i < array.count; i++) {
  117.             CCRadioButton *radioButton = nil;
  118.             switch (orientation) {
  119.                 case CCRadioGroupOrientationVertical:
  120.                     radioButton = [[CCRadioButton alloc] initWithFrame:CGRectMake(0, tempY, 0, 0) text:[array objectAtIndex:i] aligment:CCRadioButtonAligmentRight isChecked:NO];
  121.                     tempY += 10;
  122.                     break;
  123.                 case CCRadioGroupOrientationHorizental:
  124.                     radioButton = [[CCRadioButton alloc] initWithFrame:CGRectMake(i * self.frame.size.width/array.count, 0, 0, 0) text:[array objectAtIndex:i] aligment:CCRadioButtonAligmentRight isChecked:NO];
  125.                     break;
  126.             }
  127.             radioButton.tag = i;
  128.             if (i == 0) {
  129.                 _currentButton = radioButton;
  130.             }
  131.             radioButton.delegate = self;
  132.             [self addSubview:radioButton];
  133.             [_buttonArray addObject:radioButton];
  134.         }
  135.     }
  136.     return self;
  137. }

  138. - (void)checkIndex:(NSUInteger)index
  139. {
  140.     _currentButton = [_buttonArray objectAtIndex:index];
  141.     _currentButton.check = YES;
  142.     _checkedIndex = index;
  143. }

  144. - (CCRadioButton *)getCheckedRadioButton:(NSUInteger)checkId
  145. {
  146.     return (CCRadioButton *)[_buttonArray objectAtIndex:checkId];
  147. }

  148. #pragma mark - CCRadioButtonDelegate
  149. - (void)onCheckedChanged:(CCRadioButton *)button
  150. {
  151.     [_currentButton setCheck:NO];
  152.     _currentButton = button;
  153.     [_currentButton setCheck:YES];
  154.     _checkedIndex = button.tag;
  155.     if (_delegate && [_delegate respondsToSelector:@selector(onCheckedChanged:index:)]) {
  156.         [_delegate onCheckedChanged:self index:button.tag];
  157.     }
  158. }

  159. @end
  160. //RadioGroup代理
  161. #import <Foundation/Foundation.h>

  162. @class CCRadioGroup;
  163. @protocol CCRadioGroupDelegate <NSObject>

  164. - (void)onCheckedChanged:(CCRadioGroup *)radioGroup index:(NSUInteger)checkId;

  165. @end


  166. //使用实例
  167. NSArray *textArray = @[@"一月", @"半年", @"一年"];
  168.     CCRadioGroup *radioGroup = [[CCRadioGroup alloc] initWithFrame:CGRectMake(5, 2, SCREEN_WIDTH - 10, DEFAULT_HEIGHTLB) buttonTitle:textArray orientation:CCRadioGroupOrientationHorizental];
  169.     radioGroup.delegate = self;

  170. //实现代理
  171. - (void)onCheckedChanged:(CCRadioGroup *)radioGroup index:(NSUInteger)checkId
  172. {
  173.     NSLog(@"点击了 -- %@", [radioGroup getCheckedRadioButton:checkId].text);
  174. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP