美文网首页
iOS 自定义键盘字母按钮

iOS 自定义键盘字母按钮

作者: 冬日的太阳_c107 | 来源:发表于2018-11-27 10:05 被阅读0次

    最近项目有个需求24个按钮, 排列像键盘一样,获取字母进行索引搜索单词, 那就for循环添加 按钮吧, 这是封装的view, 在这里记录一下, 可以直接复制代码效果图

    :

    .h 里有个block 可以回调点击的是哪个单词这里床的是字符串

    #import

    @interfaceTKYKeyboardView :UIView

    @property(nonatomic,copy)void(^seachLetterStrbBlock)(NSString* letterStr);

    @end

    .m中具体思路是创建三个view, 第一行是一个view 第二行是一个view, 第三行字母也是一个view,然后for循环 添加字母按钮

    #import "TKYKeyboardView.h"

    #import "UIView+Extension.h"

    @interface TKYKeyboardView()

    @property (nonatomic, strong)NSMutableArray *arrM;

    @property  (nonatomic, strong)UIButton *button;

    @end

    @implementationTKYKeyboardView

    - (instancetype)initWithFrame:(CGRect)frame {

        if(self= [superinitWithFrame:frame]) {

            [selfsetUI];

        }

        return self;

    }

    -  (void)setUI{

        CGFloatspace =5;

        CGFloatx  =0;

        self.arrM = [NSMutableArray array];

         UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(20, 109, [UIScreen  mainScreen].bounds.size.width-40, 30)];

    //    oneView.backgroundColor = [UIColor yellowColor];

        [selfaddSubview:oneView];

        NSArray *oneArr = @[@"Q",@"W",@"E",@"R",@"T",@"Y",@"U",@"I",@"O",@"P"];

        for(inti=0; i

        {

            x =i*((oneView.kx_width-9*5)/10)  +i*space;

            UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];

            [pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];

            [pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];

            pressButton.tag= i;

            pressButton.backgroundColor = [UIColor redColor];

            pressButton.frame=CGRectMake(x,0, (oneView.kx_width-9*5)/10, ((oneView.kx_width-9*5)-9*5)/10);

            [self.arrMaddObject:pressButton];

            pressButton.selected=NO;

            [pressButtonsetTitle:oneArr[i] forState:UIControlStateNormal];

            [pressButtonaddTarget:self action:@selector(goPressSecondKeyButtons:) forControlEvents:UIControlEventTouchUpInside];

            [oneViewaddSubview:pressButton];

        }

        NSArray *twoArr = @[@"A",@"S",@"D",@"F",@"G",@"H",@"J",@"K",@"L"];

         UIView *twoView = [[UIView alloc]initWithFrame:CGRectMake(40, oneView.kx_bottom+space, [UIScreen  mainScreen].bounds.size.width-80, 30)];

        twoView.backgroundColor = [UIColor yellowColor];

        [selfaddSubview:twoView];

        for(inti=0; i

        {

             x =i*((twoView.kx_width-8*5)/9) +i*space;

            UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];

            [pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];

            [pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];

            pressButton.tag= i+200;

            pressButton.backgroundColor = [UIColor redColor];

           pressButton.frame=CGRectMake(x,0, (twoView.kx_width-8*space)/9, ((twoView.kx_width-8*space)-8*5)/9);

            [self.arrMaddObject:pressButton];

            pressButton.selected=NO;

            [pressButtonsetTitle:twoArr[i] forState:UIControlStateNormal];

            [pressButtonaddTarget:self action:@selector(goPressSecondKeyButtons2:) forControlEvents:UIControlEventTouchUpInside];

            [twoViewaddSubview:pressButton];

        }

       NSArray *threeArr = @[@"Z",@"X",@"C",@"V",@"B",@"N",@"M"];

        CGFloat threeView_with = [UIScreen  mainScreen].bounds.size.width-160;

        UIView *threeView = [[UIView alloc]initWithFrame:CGRectMake(80, twoView.kx_bottom+space, [UIScreen  mainScreen].bounds.size.width-160, 30)];

        [selfaddSubview:threeView];

        for(inti=0; i

        {

            x =i*((threeView_with-6*space)/7) +i*space;

            UIButton *pressButton =[UIButton buttonWithType:UIButtonTypeCustom];

            [pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 (2).png"] forState:UIControlStateNormal];

            [pressButtonsetBackgroundImage:[UIImage imageNamed:@"tu1 2.png"] forState:UIControlStateHighlighted];

            pressButton.tag= i+300;

           pressButton.backgroundColor = [UIColor redColor];

           pressButton.frame=CGRectMake(x,0, (threeView_with-6*space)/7, ((threeView_with-6*space)-6*space)/7);

            [self.arrMaddObject:pressButton];

            pressButton.selected=NO;

            [pressButtonsetTitle:threeArr[i]forState:UIControlStateNormal];

            [pressButtonaddTarget:self action:@selector(goPressSecondKeyButtons3:) forControlEvents:UIControlEventTouchUpInside];

            [threeViewaddSubview:pressButton];

        }

    }

    - (void)goPressSecondKeyButtons:(UIButton*)button{

        for(UIButton*btninself.arrM) {

            if(btn.tag== button.tag) {

                btn.backgroundColor = [UIColor grayColor];

            }else{

                btn.backgroundColor = [UIColor redColor];

            }

        }

        if (self.seachLetterStrbBlock) {

            self.seachLetterStrbBlock(button.titleLabel.text);

        }

    }

    - (void)goPressSecondKeyButtons2:(UIButton*)button{

        for(UIButton*btninself.arrM) {

            if(btn.tag== button.tag) {

                btn.backgroundColor = [UIColor grayColor];

            }else{

                btn.backgroundColor = [UIColor redColor];

            }

        }

        if (self.seachLetterStrbBlock) {

            self.seachLetterStrbBlock(button.titleLabel.text);

        }

    }

    - (void)goPressSecondKeyButtons3:(UIButton*)button{

        for(UIButton*btninself.arrM) {

            if(btn.tag== button.tag) {

                btn.backgroundColor = [UIColor grayColor];

            }else{

                btn.backgroundColor = [UIColor redColor];

           }

        }

        if (self.seachLetterStrbBlock) {

            self.seachLetterStrbBlock(button.titleLabel.text);

        }

    }

    相关文章

      网友评论

          本文标题:iOS 自定义键盘字母按钮

          本文链接:https://www.haomeiwen.com/subject/brwwqqtx.html