uiview隐藏和显示中的问题



实际上我必须使视图出现和消失一旦按钮被点击。请告诉我具体的代码来完成

happyView=[[UIView alloc]init];
happyView.frame=CGRectMake(MainView.frame.size.width*0.26,CGRectGetHeight(happyBtn.frame)*2.2, CGRectGetWidth(MainView.frame)/1.6, CGRectGetHeight(MainView.frame)/3);
happyView.layer.cornerRadius=8.0;
happyView.layer.borderWidth=0.5;
happyView.layer.borderColor=[[UIColor grayColor]CGColor];
[MainView addSubview:happyView];    
LettingGo=[[UIButton alloc]init];
LettingGo.frame=CGRectMake(happyView.frame.size.width*0.01,happyView.frame.size.height*0.1, CGRectGetWidth(happyView.frame)/1.05, 50);
[LettingGo setTitle:@"Letting go of negativity" forState:UIControlStateNormal];
LettingGo.titleLabel.numberOfLines=2;
LettingGo.titleLabel.adjustsFontSizeToFitWidth=YES;
[LettingGo setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LettingGo];
LivingPresent=[[UIButton alloc]init];
LivingPresent.frame=CGRectMake(happyView.frame.size.width*0.01,LettingGo.frame.size.height*1.4, CGRectGetWidth(happyView.frame)/1.05, 40);
[LivingPresent setTitle:@"Living in the present" forState:UIControlStateNormal];
LivingPresent.titleLabel.adjustsFontSizeToFitWidth=YES;
[LivingPresent setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LivingPresent];
happyView.hidden=YES;
...
- (void)tapHappy:(id)selector {
    happyView.hidden=NO;
}

UIButton有一个addTarget方法,用于在按钮发生动作时调用一个方法

[yourButton addTarget:self action:@selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];

当用户从按钮UIControlEventTouchUpInside抬起手指时,将在self中查找方法tapHappy:

显然你可以改变这些值来满足你的需要

但我想那应该是你要找的。

然后也许改变tapHappy方法,所以它不是总是设置YES为隐藏属性…比如

happyView.hidden=!happyView.hidden;

可以打开和关闭

当您调用tapHappy方法时检查您的happyView是否隐藏

UIButton * yourButton=[[UIButton alloc]init];
    yourButton.frame=CGRectMake(YOUR_FRAME);
    [yourButton setTitle:@"Title" forState:UIControlStateNormal];
    [yourButton addTarget:self action:@selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
    [yourButton setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
    [YourView addSubview:yourButton];


-(void)tapHappy:(id)selector{
    if(happyView.hidden)
        happyView.hidden = NO;
    else
        happyView.hidden = YES;
}

最新更新