Animating UIView doesn't working as expected

三世轮回 提交于 2019-11-29 12:40:00
Jim Tierney

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.

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

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. Setting the table's height, width and top and left space constraints within the UIView will be enough.

If you want your view to be hidden from first load, with your viewDidload set the constraint to -valueOfHeightOfBasket

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!