執行動畫所需要的工作由UIView類自動完成,但仍要在希望執行動畫時通知視圖,為此需要將改變屬性的代碼包裝到一個代碼塊中。
1.UIView動畫具體創建方法
- (void)buttonPressed
{
// 交換本視圖控制器中2個view位置
[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//UIView開始動畫,第一個參數是動畫的標識,第二個參數附加的應用程序信息用來傳遞給動畫代理消息
[UIView beginAnimations:@"View Flip" context:nil];
//動畫持續時間
[UIView setAnimationDuration:1.25];
//設置動畫的回調函數,設置后可以使用回調方法
[UIView setAnimationDelegate:self];
//設置動畫曲線,控制動畫速度
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
//設置動畫方式,并指出動畫發生的位置
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
//提交UIView動畫
[UIView commitAnimations];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//主要功能通過UIView動畫完成2個試圖控制器的切換
self.blueController = [[BlueViewController alloc] initWithNibName:nil bundle:nil];
//設置導航控制器view的大小占整個屏幕
[self.blueController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];
self.yellowController = [[YellowController alloc]initWithNibName:nil bundle:nil ];
[self.yellowController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];
//將2個控制器view插入到目前導航控制器視圖上,yellowController后插入,顯示在最前面
[self.view insertSubview:self.blueController.view atIndex:0];
[self.view insertSubview:self.yellowController.view atIndex:1];
//創建導航控制器右按鈕,按鈕名字叫next
//添加buttonPressed 事件
self.rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(buttonPressed)];
//將按鈕添加到導航控制器默認右按鈕上
self.navigationItem.rightBarButtonItem = self.rightBarItem;
}
有個問題:如果動畫不放在按鈕事件中,直接放到viewDidLoad里,程序首先執行這個controller,這時動畫是不會顯示的。
原因:出現這個問題是因為開機時候系統有個動畫,系統動畫和這個動畫重復了。
解決方案:
1。將動畫寫在按鈕事件中
2。利用定時器。
areAnimationsEnabled
返回一個布爾值表示動畫是否結束。
+ (BOOL)areAnimationsEnabled
返回值
如果動畫結束返回YES,否則NO。
beginAnimations:context:
開始一個動畫塊
+ (void)beginAnimations:(NSString *)animationID context:(void *)context
參數
animationID
動畫塊內部應用程序標識用來傳遞給動畫代理消息-這個選擇器運用setAnimationWillStartSelector:和setAnimationDidStopSelector: 方法來設置。
context
附加的應用程序信息用來傳遞給動畫代理消息-這個選擇器使用setAnimationWillStartSelector: 和setAnimation
新聞熱點
疑難解答