简体   繁体   English

将框架设置为UIView不起作用

[英]Setting the frame to a UIView not working

I need a UIView to be hidden from the screen initially and then slides up. 我需要一个UIView首先从屏幕上隐藏起来,然后向上滑动。 Kind of like a UIActionSheetView animation. 有点像UIActionSheetView动画。 In IB, I have set a UIView stick to the bottom border of the superview. 在IB中,我将UIView杆设置为UIView视图的底部边框。 In code, I have changed its frame to be hidden away from the screen when the app starts. 在代码中,我将其框架更改为在应用启动时隐藏在屏幕之外。 And when it starts, the UIView slides up. 并且在启动时, UIView向上滑动。 Here's is my code. 这是我的代码。 This optionsSheetView is the 'UIView' I'm referring to. 这个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) {

    }];
}

Now here's my problem. 现在这是我的问题。 This works just fine. 这样很好。 But I don't like keeping hard coded values in my code. 但是我不喜欢在代码中保留硬编码值。 Like 374 for the optionsSheetView's y coordinate. 类似于374的optionsSheetView的y坐标。 If I put self.optionsSheetView.frame.origin.y there, it won't work. 如果我在self.optionsSheetView.frame.origin.y放置self.optionsSheetView.frame.origin.y ,它将无法正常工作。 It's basically the same thing. 基本上是同一回事。 When I replace it with its actual value which is 374, it works fine. 当我用它的实际值374替换它时,它可以正常工作。

Why is this happening? 为什么会这样呢? Is there any way I can go about this without using magic numbers? 我有什么办法可以不用魔法数字来解决这个问题?

Thank you. 谢谢。

Try following: 请尝试以下操作:

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

    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM