UIView的动画作品故障 [英] UIView animation works faulty

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

问题描述

我使用的Objective-C。我想使表视图向下移动与动画当我添加它上面的搜索栏。这是我的code(表这里是我的表视图):

I'm using Objective-C. And I want to make the table view move down with animation when I add a search bar above it. This is my code(table here is my table view):

[UIView animateWithDuration:0.3 animations:^{
    CGRect frame = self.table.frame;
    frame.origin.y = frame.origin.y+30;
    self.table.frame = frame;
}];

但它不工作。有人能帮助我吗?

But it doesn't work. Someone can help me?

推荐答案

更改帧像这样的是不是做到这一点的最好办法,而其老气。最好的办法是让进入的视图上的自动版式制约因素之一,并改变它。为例如: - 说出你的tableview具有顶部约束定义的tableview的顶部和上海华之间的间隙 - 例如10px的。从IB拖动约束,到类。要做到这一点的最佳方式是强调对IB的左侧的约束,在包含所有你的看法等上拖动,使其在你的类的属性,就像你用按钮或标签做的名单。然后在运行时,将更改约束的.constant财产。对于如。我的继承人NSLayoutConstraint属性时,整个拖入类:

Changing frames like this is not the best way to do it, and its old fashioned. The best way is to get access to one of the autolayout constraints on the view, and change it. For eg - say your tableview has a top constraint that defines the gap between the top of the tableview and the superview - eg 10px. Drag that constraint from IB, into your class. Best way to do this is to highlight the constraint on the left hand side of IB, in the list that contains all your views etc. Drag across to make it a property in your class, just like you do with a button or label. Then at runtime, you change the .constant property of that constraint. For eg. Heres my NSLayoutConstraint property when dragged across into the class :

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topBuffer;

然后在您希望此举发生在code点,您可以更改不变的性质,动画调用的外部:

Then at the point in code where you want the move to happen, you change the constant property, OUTSIDE of the animation call :

self.topBuffer.constant = 50;  //<-- or whatever the new value is

然后你要做的就是调用[self.view layoutIfNeeded]在动画块。顺便说一句 - 注意这里用的Ive用弹簧等的动画块的bou​​ncey效果了。还要注意 - layoutIfNeeded叫你正在改变对象的上海华 - 所以self.view这里是包含的tableview我主要的SuperView

Then all you do is call [self.view layoutIfNeeded] in the animation block. By the way - note here Ive used the anim block with springs etc for a bouncey effect too. Also note - layoutIfNeeded is called on the superview of the object you are changing - so self.view here is my main superview that contains the tableview.

[UIView animateWithDuration:0.7f delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^(void) {[self.view layoutIfNeeded];} completion:nil];

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

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