我有一个包含白色 UI View 的简单叠加层 - 它又包含一个事件指示器,该指示器在等待服务器响应时显示。
服务器回复完成后,我想为 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];
}];
}
上面显示了第一行的错误,说明“uiview 没有可见界面声明选择器动画具有持续时间”。 (_overlayInner 是我想要动画的 View )
我是在叫错树 - 还是有更简单的方法来为 uiview 高度设置动画?
最佳答案
该方法是 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];
}];
documentation对于这种方法。
你可以看到声明...
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
+ 表示它是类方法而不是实例方法。
编辑
只是为了解释这是如何工作的......
[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
}];
所以...如果您有一个名为
myButton
的 View 它的当前帧等于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!");
}];
如果你想在 15 秒内将按钮的高度加倍,那么你会这样做......
[UIView animateWithDuration:15
animations:^{
myButton.frame = CGRectMake(10, 10, 100, 88);
}
completion:^(BOOL finished){
NSLog(@"The animation has now stopped!");
}];
只是一个简短的说明
如果您使用 AutoLayout 并尝试通过更改框架来设置动画,请小心。他们在一起玩得不好。如果您只是学习 iOS 并习惯于制作动画,那么请不要使用 AutoLayout。然后,您可以稍后冒险使用 AutoLayout 制作动画。
关于IOS 7 - UiView的动画高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22175566/