我使用以下代码在单击按钮时将日期选择器 View 添加到我的应用程序主视图。
- (IBAction)CalenderButton_Click:(id)sender
{
// code to add date picker -mydatepicker
//animating
[self.view addSubview:myPicker];
CGRect onScreenFrame=myPicker.frame;
CGRect offScreenFrame=onScreenFrame;
offScreenFrame.origin.y=self.view.bounds.size.height;
myPicker.frame=offScreenFrame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
myPicker.frame=onScreenFrame;
[UIView commitAnimations];
}
我需要实现的两件事是,
1.此代码从底部动画化 View ,我怎样才能从顶部动画化它?
2.如果我再次点击按钮添加日期选择器,我应该检查 View 是否已经添加,如果是,删除 subview 。
提前致谢
最佳答案
对于问题 2:使用 bool 值检查日期选择器是否已在 View 中。
isSubView
是一个 BOOL
来检查,你的 myPicker
在 subview 中。
更新:
- (IBAction)CalenderButton_Click:(id)sender
{
//animating
if(!isSubview){
isSubview = YES;
[self.view addSubview:myPicker];
/*
CGRect onScreenFrame=myPicker.frame;
CGRect offScreenFrame=onScreenFrame;
offScreenFrame.origin.y=self.view.bounds.size.height;
myPicker.frame=offScreenFrame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
myPicker.frame=onScreenFrame;
[UIView commitAnimations];
*/
// to slide your view from the top
[myPicker setFrame:CGRectMake(0, -200, 320, 200)];
[myPicker setBounds:CGRectMake(0, 0, 320, 200)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
[myPicker setFrame:CGRectMake(0, 0, 320, 200)];
[UIView commitAnimations];
}else{
isSubview = NO;
[myPicker removeFromSuperView];
}
}
关于iphone - 如何在检查是否已添加 subview 后使用相同的按钮操作关闭 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132735/