我已经使用下面的代码来定位一个 UIView
,它将容纳各种按钮等。我在按钮的 IBAction
方法中显示此 View 。
我如何才能让用户单击按钮以在默认位置显示 UIView
,然后,当用户再次单击该按钮时,移动 UIView
退出屏幕?
我是在将 UIView
移动到屏幕上还是将其关闭,还是隐藏/显示 Controller 会更好?
我喜欢移动它的想法,因为它看起来有点像向下移动(不是动画或其他任何东西)。实际上,我将如何设置动画以使其移动得更慢?
CGRect f = self.optionsuiview.frame;
f.origin.x = 0;
f.origin.y = 42;
self.optionsuiview.frame = f;
最佳答案
如果您创建按钮的属性(ctrl+拖动到头文件),您可以使用它来更改按钮的状态,这样您就可以决定 View 何时向上/向下移动。
@property (strong, nonatomic) IBOutlet UIButton *yourButton;
在您的 IBAction 方法中可能看起来像这样
-(IBAction)yourButtonPressed:(id)sender{
if (self.yourButton.selected){
self.yourButton.selected = NO;
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.optionsuiview.frame;
f.origin.x = 0;
f.origin.y = 42;
self.optionsuiview.frame = f;
}];
}
if (!self.yourButton.selected){
self.yourButton.selected = YES;
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.optionsuiview.frame;
f.origin.x = 0;
f.origin.y = 0;
self.optionsuiview.frame = f;
}];
}
我希望这种方式对您正在寻找的东西有所帮助,
干杯,吉姆。
关于ios - 显示和隐藏 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798250/