简体   繁体   English

动画UIView无法正常工作

[英]Animating UIView doesn't working as expected

In my application i am using a UIView which includes UITableView , Buttons and Labels in it. 在我的应用程序中,我使用的UIView包含UITableViewButtonsLabels It created using Storyboard . 它是使用Storyboard创建的。 When user click a navigation bar button UIView will appear with animation from top to certain height and if they click it again it hides the UIView with animation(From that height to top). 当用户单击导航栏按钮时, UIView将从顶部到特定高度显示动画,如果再次单击它,则将隐藏具有动画的UIView(从该高度到顶部)。 Same like UIActionView . UIActionView一样。

It works fine if there is no records in UITableView . 如果UITableView没有记录,它将很好地工作。 But if it has any records, while calling [self hideBasket] the UIView appears from bottom of the view to top(Not Hidden). 但是,如果有任何记录,则在调用[self hideBasket] ,UIView将从视图的底部显示到顶部(未隐藏)。

// Hide Basket Code //隐藏购物篮代码

-(void)hideBasket{
    /*Finished Hiding the Basket
     [self.view sendSubviewToBack:_shoppingCartView];
     [_shoppingCartView setHidden:YES];
     _isShoppingCartSeen = NO;*/

    CGRect basketFrame = _shoppingCartView.frame;
    basketFrame.origin.y = -basketFrame.size.height;

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _shoppingCartView.frame = basketFrame;
    } completion:^(BOOL finished) {
        // Finished Hiding the Basket
        //[self.view sendSubviewToBack:_shoppingCartView];
       // [_shoppingCartView setHidden:YES];
        _isShoppingCartSeen = NO;
}]; 

// Show Basket Code //显示购物篮代码

-(void)showBasket{

    /*[self.view bringSubviewToFront:_shoppingCartView];
    [_shoppingCartView setHidden:NO];
    _isShoppingCartSeen = YES;*/

    CGRect basketFrame = _shoppingCartView.frame;
    basketFrame.origin.y = 0;

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _shoppingCartView.frame = basketFrame;
    } completion:^(BOOL finished) {
        // Finished Showing the Basket
        [self.view bringSubviewToFront:_shoppingCartView];
        [_shoppingCartView setHidden:NO];
        _isShoppingCartSeen = YES;
    }];
}

What i am doing wrong here? 我在这里做错了什么?

Using Auto-Layout you should animate your constraints, not change the frame of objects. 使用自动布局时,应设置约束动画,而不更改对象框架。

I've mocked up a rough example of where to begin using constraints, which should solve your issue 我模拟了一个从何处开始使用约束的粗略示例,这应该可以解决您的问题

Firstly you need to set the constraints of your basket view 首先,您需要设置购物篮视图的约束

Each object has to have at least 4 constraints set in order to be set properly. 每个对象必须至少设置4个约束才能正确设置。

See screenshot below, pressing the constraints icon at the bottom of the view I've chosen to set the width and height of the view, plus the left distance constraint. 请参见下面的屏幕快照,按一下我选择设置视图的宽度和高度以及左距离约束的视图底部的约束图标。

You will then need to set the space to top of superview, see second screen shot. 然后,您需要将空间设置为超级视图的顶部,请参见第二个屏幕截图。

在此处输入图片说明

Setting the constraint to top of superview 将约束设置为超级视图的顶部

在此处输入图片说明

Once your constraints have been set up you set CTRL drag the top space to superview property to your header file like the screenshot below. 设置好约束后,您可以设置CTRL并将超级视图属性的顶部空间拖到您的头文件中,如下图所示。 (you'll need to set your constraints within the view to accommodate your table objet etc too), (您还需要在视图中设置约束以适应表对象等),

在此处输入图片说明

Now that this has been set up, please replace your code with the following and it should work fine 现在已经设置好了,请用以下代码替换您的代码,它应该可以正常工作

-(void)hideBasket{

self.topVerticalSpaceConstraint.constant = -312;

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

    [self.view layoutIfNeeded];

} completion:^(BOOL finished) {

}];

} }

-(void)showBasket{ -(void)showBasket {

self.topVerticalSpaceConstraint.constant = 0;

    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.view layoutIfNeeded];

    } completion:^(BOOL finished) {

    }];

} }

Please note I've simply set the constant amount manually here to the size of the dummy view I made up, though you would of course change this to be the size of your view etc. 请注意,我只是在此处手动将常量设置为我制作的虚拟视图的大小,尽管您当然可以将其更改为您的视图等的大小。

Please remember each of your views/objects should ideally have their constraints set, especially the UITableview within your drop-down view. 请记住,理想情况下,应该为每个视图/对象设置约束,尤其是下拉视图中的UITableview。 Setting the table's height, width and top and left space constraints within the UIView will be enough. 在UIView中设置表格的高度,宽度以及顶部和左侧空间限制就足够了。

If you want your view to be hidden from first load, with your viewDidload set the constraint to -valueOfHeightOfBasket 如果要使视图在第一次加载时隐藏,请使用viewDidload将约束设置为-valueOfHeightOfBasket

I hope this helps. 我希望这有帮助。

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

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