[英]IOS 7 - Animate height of UiView
我有一個簡單的疊加層,其中包含一個白色的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];
}];
}
上面顯示了第一行的錯誤,指出“ uiview沒有可見的接口聲明選擇器具有持續時間的動畫”。 (_overlayInner是我要設置動畫的視圖)
我是否在吠錯了樹-還是有一種更簡單的方法來為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];
}];
此方法的文檔 。
您可以看到聲明...
+ (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
的視圖,並且其當前幀等於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!");
}];
只是一個簡短的說明
如果您正在使用自動版式並嘗試通過更改框架來對內容進行動畫處理,請小心。 他們在一起玩的不好。 如果您只是在學習iOS並習慣於制作動畫,則可以不使用AutoLayout來玩。 然后,您可以稍后冒險使用AutoLayout進行動畫處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.