一、CATransiton動畫(轉場動畫)
1、CATransiton是對UIView進行更低層次的控制
2、CATransiton不同于UIView動畫,它是作用域UIView的layer層
3、CATransition主要用于兩個試圖切換過渡時的動畫
二、使用CATrasition做試圖切換動畫
CATransition *animation = [CATransition animation];
//監聽動畫的開始和結束
animation.delegate = self;
animation.duration = 1.0;
//設置動畫的快慢效果
animation.timingFunction = UIViewAnimationCurveEaseInOut;
//動畫類型說明
//公有API
kCATransitionFade 交叉淡化過渡,新視圖逐漸顯示在屏幕上,舊視圖逐漸淡化出視野
kCATransitionMoveIn 新視圖移到舊視圖上面,好像蓋在上面。
kCATransitionPush 新視圖將舊視圖推出去。
kCATransitionReveal 將舊視圖移開顯示出下面的新視圖。
//私有API,注意對于蘋果官方沒公開的動畫類型只能使用字符串,并沒有對應的常量定義
animation.type =? @"cube"? //立方體效果
animation.type =? @"suckEffect" //收縮效果,如一塊布被抽走
animation.type =? @"oglFlip"? //上下翻轉效果
animation.type =? @"rippleEffect"? //滴水效果
animation.type =? @"pageCurl"? //向上翻一頁
animation.type =? @"pageUnCurl"? //向下翻一頁
animation.type =? @“cameralIrisHollowOpen”? //攝像頭打開效果
animation.type =? @“cameraIrisHollowClose”? //攝像頭關閉效果
animation.type = @"cameraIrisHollowClose";
//設置動畫子類型
animation.subtype = kCATransitionFromTop;
//獲取到子試圖在父試圖上的位置
NSInteger indexOfmyView1 = [self.view.subviews indexOfObject:myView1];
NSInteger indexOfmyView2 = [self.view.subviews indexOfObject:myView2];
//父試圖上兩個子試圖的切換
[self.view exchangeSubviewAtIndex:indexOfmyView1 withSubviewAtIndex:indexOfmyView2];
//將動畫添加到父試圖上
[self.view.layer addAnimation:animation forKey:@"test"];
PS: 給導航控制器的根視圖添加動畫,更改導航的轉換效果
[self.navigationController.view.layer addAnimation:transition forKey:@"animation"];
三、CATrasition的代理方法
//動畫開始
- (void)animationDidStart:(CAAnimation *)theAnimation
//動畫結束
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
三、練習
1、實現無限輪播
-(void)transitionAnimation:(BOOL)isNext{
//1.創建轉場動畫對象
CATransition *transition=[[CATransition alloc]init];
//2.設置動畫類型,注意對于蘋果官方沒公開的動畫類型只能使用字符串,并沒有對應的常量定義
transition.type=@"cube";
//設置子類型
if (isNext) {
transition.subtype=kCATransitionFromRight;
}else{
transition.subtype=kCATransitionFromLeft;
}
//設置動畫時常
transition.duration=1.0f;
//3.設置轉場后的新視圖添加轉場動畫
_imageView.image=[self getImage:isNext];
[_imageView.layer addAnimation:transition forKey:@"KCTransitionAnimation"];
}
-(UIImage *)getImage:(BOOL)isNext{
if (isNext) {
_currentIndex=(_currentIndex+1)%IMAGE_COUNT;
}else{
_currentIndex=(_currentIndex-1+IMAGE_COUNT)%IMAGE_COUNT;
}
NSString *imageName=[NSString stringWithFormat:@"%02d.jpg",_currentIndex];
return [UIImage imageNamed:imageName];
}
四、開源框架,HMGLTransitions
1、簡單介紹
1)下載地址:https://github.com/Split82/HMGLTransitions
2)附加了多種3D動畫效果
3)基于OpenGL封裝的一個開源動畫庫,OpenGL ES 是可以在iphone上實現2D和3D圖形編程的低級API
4)使用HMGLTransitions,必須OpenGLES.framework和 QuartzCore.framework
5)MRC框架,需配置工程,能混編-fno-objc-arc
2、使用框架切換動畫
//一種動畫效果對象
Switch3DTransition *t1 = [[Switch3DTransition alloc] init];
//設置動畫效果對象
[[HMGLTransitionManager sharedTransitionManager] setTransition:t1];
UIView *parentView = view1.superview;
//設置動畫視圖
[[HMGLTransitionManager sharedTransitionManager] beginTransition:parentView];
//兩個視圖間切換
view2.frame = view1.frame;
[view1 removeFromSuperview];
[parentView addSubview:view2];
[[HMGLTransitionManager sharedTransitionManager] commitTransition];
3、模態試圖彈出動畫
//定義動畫對象
HMGLTransition *t = [[Switch3DTransition alloc] init];
ModalViewController *modal = [[ModalViewController alloc] init];
[[HMGLTransitionManager sharedTransitionManager] setTransition:t];
//打開模態窗口
[[HMGLTransitionManager sharedTransitionManager]presentModalViewController:modal onViewController:self];
關閉模態窗口
[[HMGLTransitionManager sharedTransitionManager] setTransition:transition];
[[HMGLTransitionManager sharedTransitionManager]dismissModalViewController:self];