zoukankan      html  css  js  c++  java
  • 循环btn上面的视图

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        UIButton * btn = [self createBtn:YES andCGRectMake:CGRectMake(100, 100, 100, 100)];
        [btn setTitle:@"长度" forState:UIControlStateNormal];
    
        [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor cyanColor];
        btn.tag = 1;
        [self.view addSubview:btn];
        
        UIButton * btn1 = [self createBtn:YES andCGRectMake:CGRectMake(100, 250, 100, 100)];
        btn1.tag = 2;
        btn1.selected = NO;
        [btn1 setTitle:@"长度长度" forState:UIControlStateNormal];
        
        //循环btn  btn的背景图片不能更改
        /*
        UIImageView * image = [[UIImageView alloc]init];
        image.backgroundColor = [UIColor orangeColor];
        UIImageView * image1 = [[UIImageView alloc]init];
        image1.backgroundColor = [UIColor yellowColor];
        [btn1 setImage:image.image forState:UIControlStateNormal];
        [btn1 setImage:image1.image forState:UIControlStateSelected];
    */
        [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn1.backgroundColor = [UIColor cyanColor];
        [self.view addSubview:btn1];
    }
    
    -(void)pressBtn:(UIButton*)sender{
         //循环btn btn的背景图片不能更改
        if (sender.tag == 1) {
            for (id obj in sender.subviews) {
                if ([obj isKindOfClass:[UIImageView class]])
                {
                    if (sender.selected) {
                        UIImageView* image = (UIImageView*)obj;
                        image.backgroundColor = [UIColor redColor];
    
                        sender.selected = NO;
                    }
                    else{
                        UIImageView* image = (UIImageView*)obj;
                        image.backgroundColor = [UIColor grayColor];
                        sender.selected = YES;
                    }
                }
            }
        }
        else{
            for (id obj in sender.subviews) {
                if ([obj isKindOfClass:[UIImageView class]])
                {
                    if (sender.selected) {
                        UIImageView* image = (UIImageView*)obj;
                        image.backgroundColor = [UIColor redColor];
    
                        sender.selected = NO;
                    }
                    else{
                        UIImageView* image = (UIImageView*)obj;
                        image.backgroundColor = [UIColor grayColor];
                        sender.selected = YES;
                        
                    }
                }
            }
        }
    }
    -(UIButton*)createBtn:(BOOL)yes andCGRectMake:(CGRect)frame{
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = frame;
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(btn.titleLabel.frame.size.width/2+btn.frame.size.width/2+20, 30, 30, 30)];
        image.backgroundColor = [UIColor redColor];
        [btn addSubview:image];
        
        return btn;
        
    }

  • 相关阅读:
    [华为]计算字符串的相似度
    Java继承和组合
    Java多态(注意事项)
    Eclipse快捷键
    求二叉树中节点的最大距离
    设计模式-工厂模式
    设计模式-单例模式
    滴滴校招0910
    八大排序算法之七-归并排序
    单链表的实现(创建+排序(选择))
  • 原文地址:https://www.cnblogs.com/sayimba/p/5698358.html
Copyright © 2011-2022 走看看