实际上,我必须在点击按钮后显示和消失视图。请告诉我完成任务的具体代码
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;
}
答案 0 :(得分:1)
UIButton有一个方法addTarget,可以在按钮上发生操作时调用方法
[yourButton addTarget:self action:@selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
当用户从“UIControlEventTouchUpInside”按钮上抬起手指时,这将搜索“self”中的类以获取方法“tapHappy:”
显然,您可以根据需要更改这些值
但那应该是你想要的......我想..
然后可能更改tapHappy方法,因此它不会总是将YES设置为隐藏属性...可能类似
happyView.hidden=!happyView.hidden;
这将打开和关闭
答案 1 :(得分:0)
当您致电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;
}