IOS 7 - UiView的动画高度

Dan*_*cer 1 objective-c ios

我有一个简单的叠加层,其中包含一个白色UI视图 - 它又包含一个活动指示器,在等待服务器响应时显示.

当服务器回复完成后,我想设置uiView的高度动画,并在高度动画完成后显示隐藏的返回按钮 -

到目前为止我已经得到了以下内容(我已经改编自另一篇文章) - 但我不确定我是否在正确的轨道上!?

- (void) showAnimation
{
    [_overlayInner animateWithDuration:60
                 animations:^{
                     CGRect frame = left.frame;
                     // adjust size of frame to desired value
                     frame.size.height = 120;
                     left.frame = frame; // set frame on your view to the adjusted size
                 }
                 completion:^(BOOL finished){
                     [_rtnBtn setHidden:NO];
                 }];
}
Run Code Online (Sandbox Code Playgroud)

上面显示第一行的错误,声明'uiview的可见界面没有声明选择器动画有持续时间'.(_overlayInner是我想要制作动画的视图)

我是在吠叫错误的树 - 还是有更简单的方法来制作uiview高度?

Fog*_*ter 8

该方法是一种类方法UIView.

你应该这样称呼它......

[UIView animateWithDuration:60
             animations:^{
                 CGRect frame = left.frame;
                 // adjust size of frame to desired value
                 frame.size.height = 120;
                 left.frame = frame; // set frame on your view to the adjusted size
             }
             completion:^(BOOL finished){
                 [_rtnBtn setHidden:NO];
             }];
Run Code Online (Sandbox Code Playgroud)

此方法的文档.

你可以看到声明......

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Run Code Online (Sandbox Code Playgroud)

+表示它是类方法而不是实例方法.

编辑

只是为了解释这是如何工作的......

[UIView animateWithDuration:60 //this is the length of time the animation will take
             animations:^{
                 //this is where you change the stuff to its "final" state
             }
             completion:^(BOOL finished){
                 //this gets run after the animation is complete
             }];
Run Code Online (Sandbox Code Playgroud)

所以...如果你有一个被调用的视图myButton并且它的当前帧是等于CGRectMake(10, 10, 100, 44)你想要在5秒内向右边动画20点,然后登录控制台,动画已停止,那么你可以做...

[UIView animateWithDuration:5
             animations:^{
                 myButton.frame = CGRectMake(30, 10, 100, 44);
             }
             completion:^(BOOL finished){
                 NSLog(@"The animation has now stopped!");
             }];
Run Code Online (Sandbox Code Playgroud)

如果你想在15秒内将按钮的高度加倍,那么你会......

[UIView animateWithDuration:15
             animations:^{
                 myButton.frame = CGRectMake(10, 10, 100, 88);
             }
             completion:^(BOOL finished){
                 NSLog(@"The animation has now stopped!");
             }];
Run Code Online (Sandbox Code Playgroud)

快速说明一下

如果您正在使用AutoLayout并尝试通过更改帧来设置动画效果,请务必小心.他们不能很好地在一起玩.如果您只是在学习iOS并习惯于制作动画,那么请不要使用AutoLayout.然后,您可以稍后使用AutoLayout进行动画制作.