将框架设置为UIView不起作用

时间:2014-05-03 21:55:08

标签: ios objective-c uiview uiviewanimation

我需要先从屏幕隐藏UIView,然后向上滑动。有点像UIActionSheetView动画。在IB中,我已经设置了一个UIView棍子到superview的底部边框。在代码中,当应用程序启动时,我已将其框架更改为远离屏幕隐藏。当它开始时,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替换它时,它工作正常。

为什么会这样?如果不使用魔术数字,我有什么方法可以解决这个问题吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

- (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) {

    }];
}
相关问题