zoukankan      html  css  js  c++  java
  • ios中radiobutton

    #import <UIKit/UIKit.h>
    
    @protocol RadioButtonExtDelegate;
    
    @interface RadioButtonExt : UIView
    
    - (id)initWithFrame:(CGRect)frame data:(NSArray *)data ;
    @property(nonatomic,assign)id<RadioButtonExtDelegate> delegate;
    @end
    
    @protocol RadioButtonExtDelegate <NSObject>
    
    -(void)RadioButton:(RadioButtonExt *)rd from:(NSInteger)from to:(NSInteger)to;
    
    @end
    
    
    #import "RadioButtonExt.h"
    #import "CommonButton.h"
    #define KCount 2//要显示个数
    #define KMinTag 100
    
    @interface RadioButtonExt ()
    {
        CommonButton *_LastExt;
    }
    @end
    
    @implementation RadioButtonExt
    
    - (id)initWithFrame:(CGRect)frame data:(NSArray *)data 
    {
        self = [super initWithFrame:frame];
        if (self) {
            int count=data.count;
            int width=frame.size.width/KCount;//没一个宽度
            int row=count%2==0?count/KCount:count/KCount+1;//多少行
            int height=frame.size.height/row;//每一行的高度
            CGRect Rowrec,hrect,rect=self.bounds;
            for (int i=0; i<row; i++) {
                CGRectDivide(rect, &Rowrec, &rect, height, CGRectMinYEdge);//分割row
                for (int j=0; j<KCount; j++) {
                    if (KCount*i+j>=count) {//行中个数
                        break;
                    }
                    CGRectDivide(Rowrec, &hrect, &Rowrec, width, CGRectMinXEdge);
                    CommonButton *rd=[[CommonButton alloc] initWithFrame:hrect];
                    [rd addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
                    NSString *normalBg=[[NSBundle mainBundle]pathForResource:@"radio_normal" ofType:@"png"];
                    NSString *seletctBg=[[NSBundle mainBundle]pathForResource:@"radio_selected" ofType:@"png"];
                    
                    [rd setImage:[UIImage imageWithContentsOfFile:normalBg] forState:UIControlStateNormal];
                    [rd setImage:[UIImage imageWithContentsOfFile:seletctBg] forState:UIControlStateSelected];
                    rd.tag=KMinTag+KCount*i+j;
                    [rd setTitle:data[KCount*i+j] forState:UIControlStateNormal];
                    [self addSubview:rd];
                }
            }
        
        }
        return self;
    }
    
    -(void)click:(CommonButton *)btn{
        if (_LastExt!=btn) {
            _LastExt.selected=NO;
            btn.selected=YES;
            
            if ([self.delegate respondsToSelector:@selector(RadioButton:from:to:)]) {
                [self.delegate RadioButton:self from:_LastExt.tag-KMinTag to:btn.tag-KMinTag];
            }
            _LastExt=btn;
        }
        
    }
  • 相关阅读:
    [转]暴风电视开机卡死、闪屏怎么办
    暴风电视快速查询机器型号及平台
    暴风电视风行系统FUNOS插入U盘、移动硬盘不能写入文件。
    yum版本号前有:冒号 指的是依赖版本号,默认0不显示
    yum多个源repo安装指定版本docker
    [转]YUM的工作机制与配置
    yum!base仓库里的repo id(源标识)前有叹号
    Docker新旧版本号下载
    yum没有可用软件包 docker。错误:无须任何处理CentOS-Media.repo仓库
    【笔记整理】之 servlet
  • 原文地址:https://www.cnblogs.com/gcb999/p/3290698.html
Copyright © 2011-2022 走看看