button分次点击效果不同(点击button隐藏一个视图,再次点击该视图显示,如此循环)

//创建一个button,添加在控制器视图

UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];

    addButton.backgroundColor = [UIColor clearColor];
    addButton.frame = CGRectMake(0, 440, 40, 40);
    [addButton addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:addButton];

//创建一个视图,视图上可以放置其他button,从而达到节省空间的效果.其中.aView为uiview类的属性.

self.aView = [[UIView alloc] initWithFrame:CGRectMake(40, 420, 320, 60)];
    self.aView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.aView];
    self.aView.hidden = YES;

//触发的方法

实现机理为循环改变视图的隐藏性.

- (void)add:(UIButton *)button
{
    if (self.aView.hidden == NO) {
        self.aView.hidden = YES;
        
    }else{
        self.aView.hidden = NO;
    }
    
}

原文地址:https://www.cnblogs.com/xukunhenwuliao/p/3576212.html