动画UIView没有按预期工作 [英] Animating UIView doesn't working as expected

查看:136
本文介绍了动画UIView没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用 UIView ,其中包括 UITableView 按钮标签。它使用 Storyboard 创建。当用户单击导航栏按钮 UIView 将显示动画从顶部到特定高度,如果再次单击它,则会隐藏带有动画的UIView(从该高度到顶部)。
UIActionView 相同。



如果的UITableView 。但如果它有任何记录,在调用 [self hideBasket] 时,UIView会从视图底部显示到顶部(非隐藏)。



//隐藏篮子代码

   - (无效)hideBasket {
/ *已完成隐藏篮子
[self.view sendSubviewToBack:_shoppingCartView];
[_shoppingCartView setHidden:YES];
_isShoppingCartSeen = NO; * /

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

[UIView animateWithDuration:1.0延迟:0.0选项:UIViewAnimationOptionCurveEaseOut动画:^ {
_shoppingCartView.frame = basketFrame;
}完成:^(BOOL完成){
//完成隐藏篮子
// [self.view sendSubviewToBack:_shoppingCartView];
// [_shoppingCartView setHidden:YES];
_isShoppingCartSeen = NO;
}];

//显示篮子代码

   - (void)showBasket {

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

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

[UIView animateWithDuration:1.0延迟:0.0选项:UIViewAnimationOptionCurveEaseOut动画:^ {
_shoppingCartView.frame = basketFrame;
}完成:^(BOOL完成){
//完成显示篮子
[self.view bringSubviewToFront:_shoppingCartView];
[_shoppingCartView setHidden:NO];
_isShoppingCartSeen = YES;
}];
}

我在这里做错了什么?

解决方案

使用自动布局,您应该为约束设置动画,而不是更改对象的框架。



我'我们嘲笑了一个粗略的例子,说明从哪里开始使用约束,这应该可以解决你的问题



首先你需要设置篮子视图的约束



每个对象必须至少设置4个约束才能正确设置。



请参阅下面的截图,按下我选择的视图底部的约束图标来设置视图的宽度和高度,加上左边距离约束。 / p>

然后您需要将空间设置为超视图的顶部,请参阅第二个屏幕截图。





将约束设置为顶部superview





设置约束后,设置CTRL将顶部空间拖动到superview属性到您的头文件,如下面的屏幕截图所示。
(您需要在视图中设置约束以适应您的表格对象等),





现在已经设置好了,请用以下代码替换您的代码它应该可以正常工作

   - (void)hideBasket {

self.topVerticalSpaceConstraint.constant = - 312;

[UIView animateWithDuration:1.0延迟:0.0选项:UIViewAnimationOptionCurveEaseOut动画:^ {

[self.view layoutIfNeeded];

}完成:^(BOOL完成){

}];

}



- (void)showBasket {

  self.topVerticalSpaceConstraint.constant = 0; 

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

}完成:^(BOOL完成){

}];

}



请注意我只是在这里手动设置恒定量到我组成的虚拟视图的大小,但你当然会将其改为视图的大小等。



请记住,理想情况下,每个视图/对象都应设置约束,尤其是下拉视图中的UITableview。在UIView中设置表的高度,宽度以及顶部和左侧空间限制就足够了。



如果希望在第一次加载时隐藏视图,请设置viewDidload对-valueOfHeightOfBasket的约束



我希望这会有所帮助。


In my application i am using a UIView which includes UITableView,Buttons and Labels in it. It created using 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). Same like UIActionView.

It works fine if there is no records in UITableView. But if it has any records, while calling [self hideBasket] the UIView appears from bottom of the view to top(Not Hidden).

// 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.

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.

这篇关于动画UIView没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆