您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 7 手勢樣式轉場下 UIView 動畫失效的排查與替代方案

編輯:iOS 開發綜合

近期想製作一個跑馬燈效果,很自然地寫出了下面的程式碼。測試時發現一件怪事:同樣的邏輯在 iOS 6 上動畫正常播放,到了 iOS 7 卻完全不動,畫面就是停在初始狀態。

[_scrollLabel sizeToFit];
CGRect frame = _scrollLabel.frame;
frame.origin.x = 320;
_scrollLabel.frame = frame;

[UIView setAnimationsEnabled:YES];
[UIView beginAnimations:@"testAnimation" context:NULL];
[UIView setAnimationDuration:10.f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationRepeatCount:999999];

frame = _scrollLabel.frame;
frame.origin.x = -frame.size.width;
NSLog(@"frame orgin:%f", frame.origin.x);
_scrollLabel.frame = frame;
[UIView commitAnimations];

翻遍網路資料都沒能解決,最後才定位到真正的關鍵:這個視圖控制器是用 present 模態方式呈現的,而在 iOS 7 下,模態呈現出來的視圖上跑 UIView 動畫會出現問題。

於是果斷把 present 換成 push。但 push 又拿不到原本想要的模態轉場觀感,只好手動在 push 的視圖裡模擬 present 的動畫效果。

一、push 時模擬 present

MCLotteryListViewController *list = [[MCLotteryListViewController alloc] init];

// 原本使用的模態寫法,在 iOS 7 下會導致子視圖動畫失效
// [self presentViewController:list animated:YES completion:^{ }];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController:list animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
                       forView:self.navigationController.view
                         cache:NO];
[UIView commitAnimations];

二、pop 時對稱收尾

返回動畫需要拆成兩段:先播放捲落轉場,等一半時間後再真正執行 pop,這樣視覺上才不會突兀。

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
                       forView:self.navigationController.view
                         cache:NO];
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:0.375];
[self.navigationController popViewControllerAnimated:NO];
[UIView commitAnimations];

整體思路就是:既然系統提供的模態容器在該版本上有相容性問題,那就回到自己掌控動畫節奏的老路上,用轉場動畫搭配延遲執行來還原預期效果。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved