我需要一个 UIView
最初从屏幕上隐藏然后向上滑动。有点像 UIActionSheetView
动画。在 IB 中,我设置了一个 UIView
贴在 super View 的底部边框上。在代码中,我将其框架更改为在应用程序启动时隐藏在屏幕之外。当它启动时,UIView
会向上滑动。这是我的代码。此 optionsSheetView 是我指的“UIView”。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Hide the UIView from the screen initially
self.optionsSheetView.frame = CGRectMake(0, self.optionsSheetView.frame.origin.y + self.optionsSheetView.frame.size.height, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
// Sliding up animation
[UIView animateWithDuration:0.9 delay:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.optionsSheetView.frame = CGRectMake(0, 374, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
} completion:^(BOOL finished) {
}];
}
现在我的问题来了。这很好用。但我不喜欢在我的代码中保留硬编码值。对于 optionsSheetView 的 y
坐标,例如 374。如果我将 self.optionsSheetView.frame.origin.y
放在那里,它将不起作用。基本上是一样的。当我用它的实际值 374 替换它时,它工作正常。
为什么会这样?有什么办法可以在不使用魔数(Magic Number)的情况下解决这个问题吗?
谢谢。
最佳答案
尝试以下操作:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGFloat oldY = self.optionsSheetView.frame.origin.y;
// Hide the UIView from the screen initially
self.optionsSheetView.frame = CGRectMake(0, self.optionsSheetView.frame.origin.y + self.optionsSheetView.frame.size.height, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
// Sliding up animation
[UIView animateWithDuration:0.9 delay:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.optionsSheetView.frame = CGRectMake(0, oldY, self.optionsSheetView.frame.size.width, self.optionsSheetView.frame.size.height);
} completion:^(BOOL finished) {
}];
}
关于ios - 将框架设置为 UIView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23450220/