美文网首页
ios 获取手势点击视图的tag及随机色

ios 获取手势点击视图的tag及随机色

作者: 缘來諟夢 | 来源:发表于2020-12-22 12:26 被阅读0次
    //随机色
    #define random(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]
    #define randomColor random(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))
    
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        for (int i = 0; i < 3; i ++) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + (100 + 17) * i, 100, 100, 50)];
            view.backgroundColor = [UIColor greenColor];
            view.tag = 10 + i;
            [self.view addSubview:view];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getViewTag:)];
            [view addGestureRecognizer:tap];
          
        }
    }
     
    - (void)getViewTag:(UITapGestureRecognizer *)sender{
       UIView *getView = sender.view;
        NSLog(@"%ld", getView.tag);
        getView.backgroundColor = randomColor;
    }
    
    

    相关文章

      网友评论

          本文标题:ios 获取手势点击视图的tag及随机色

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