ios - 将框架设置为 UIView 不起作用

标签 ios objective-c uiview uiviewanimation

我需要一个 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/

相关文章:

ios - 为什么这些数字没有添加到 Xcode 调试器中?

ios - 背景图像重叠刷新控件

iPhone : How to manage 130 images in ImageView?

iphone - 如何从一个 View 复制一个字符串并在下一个 View 中显示它?(iphone)

ios - Objective-C - 使用 Accelerate.framework 对两个矩阵进行逐元素加法(和除法)

iPhone 程序在模拟器上运行但不在 iPhone 上运行

objective-c - 下载iCloud文件的方法?非常困惑?

ios - 由于仅变换层而在 UIView 中发出警告

iphone - 以编程方式将自动布局约束添加到恒定宽度和高度的 subview

ios - UITableViewCell 详细文本标签布局不当