一、CATransiton動畫(轉(zhuǎn)場動畫)
1、CATransiton是對UIView進(jìn)行更低層次的控制
2、CATransiton不同于UIView動畫,它是作用域UIView的layer層
3、CATransition主要用于兩個試圖切換過渡時的動畫
二、使用CATrasition做試圖切換動畫
CATransition *animation = [CATransition animation];
//監(jiān)聽動畫的開始和結(jié)束
animation.delegate = self;
animation.duration = 1.0;
//設(shè)置動畫的快慢效果
animation.timingFunction = UIViewAnimationCurveEaseInOut;
//動畫類型說明
//公有API
kCATransitionFade 交叉淡化過渡,新視圖逐漸顯示在屏幕上,舊視圖逐漸淡化出視野
kCATransitionMoveIn 新視圖移到舊視圖上面,好像蓋在上面。
kCATransitionPush 新視圖將舊視圖推出去。
kCATransitionReveal 將舊視圖移開顯示出下面的新視圖。
//私有API,注意對于蘋果官方?jīng)]公開的動畫類型只能使用字符串,并沒有對應(yīng)的常量定義
animation.type = @"cube" //立方體效果
animation.type = @"suckEffect" //收縮效果,如一塊布被抽走
animation.type = @"oglFlip" //上下翻轉(zhuǎn)效果
animation.type = @"rippleEffect" //滴水效果
animation.type = @"pageCurl" //向上翻一頁
animation.type = @"pageUnCurl" //向下翻一頁
animation.type = @“cameralIrisHollowOpen” //攝像頭打開效果
animation.type = @“cameraIrisHollowClose” //攝像頭關(guān)閉效果
animation.type = @"cameraIrisHollowClose";
//設(shè)置動畫子類型
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: 給導(dǎo)航控制器的根視圖添加動畫,更改導(dǎo)航的轉(zhuǎn)換效果
[self.navigationController.view.layer addAnimation:transition forKey:@"animation"];
三、CATrasition的代理方法
//動畫開始
- (void)animationDidStart:(CAAnimation *)theAnimation
//動畫結(jié)束
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
三、練習(xí)
1、實(shí)現(xiàn)無限輪播
-(void)transitionAnimation:(BOOL)isNext{
//1.創(chuàng)建轉(zhuǎn)場動畫對象
CATransition *transition=[[CATransition alloc]init];
//2.設(shè)置動畫類型,注意對于蘋果官方?jīng)]公開的動畫類型只能使用字符串,并沒有對應(yīng)的常量定義
transition.type=@"cube";
//設(shè)置子類型
if (isNext) {
transition.subtype=kCATransitionFromRight;
}else{
transition.subtype=kCATransitionFromLeft;
}
//設(shè)置動畫時常
transition.duration=1.0f;
//3.設(shè)置轉(zhuǎn)場后的新視圖添加轉(zhuǎn)場動畫
_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上實(shí)現(xiàn)2D和3D圖形編程的低級API
4)使用HMGLTransitions,必須OpenGLES.framework和 QuartzCore.framework
5)MRC框架,需配置工程,能混編-fno-objc-arc
2、使用框架切換動畫
//一種動畫效果對象
Switch3DTransition *t1 = [[Switch3DTransition alloc] init];
//設(shè)置動畫效果對象
[[HMGLTransitionManager sharedTransitionManager] setTransition:t1];
UIView *parentView = view1.superview;
//設(shè)置動畫視圖
[[HMGLTransitionManager sharedTransitionManager] beginTransition:parentView];
//兩個視圖間切換
view2.frame = view1.frame;
[view1 removeFromSuperview];
[parentView addSubview:view2];
[[HMGLTransitionManager sharedTransitionManager] commitTransition];
3、模態(tài)試圖彈出動畫
//定義動畫對象
HMGLTransition *t = [[Switch3DTransition alloc] init];
ModalViewController *modal = [[ModalViewController alloc] init];
[[HMGLTransitionManager sharedTransitionManager] setTransition:t];
//打開模態(tài)窗口
[[HMGLTransitionManager sharedTransitionManager]presentModalViewController:modal onViewController:self];
關(guān)閉模態(tài)窗口
[[HMGLTransitionManager sharedTransitionManager] setTransition:transition];
[[HMGLTransitionManager sharedTransitionManager]dismissModalViewController:self];
一、UIView動畫
1、基礎(chǔ)動畫
//標(biāo)記動畫塊開始
[UIView beginAnimations:nil context:nil];
//定義動畫加速和減速方式
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//以秒為單位指定動畫時長
[UIView setAnimationDuration:0.5];
//設(shè)置代理
[UIView setAnimationDelegate:self];
//動畫結(jié)束后回調(diào)方法
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
/*視圖變化動作在這里*/
//標(biāo)志動畫塊的結(jié)束
[UIView commitAnimations];
2、試圖切換的過渡動畫
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.6];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:parentView cache:YES];
//在parentView執(zhí)行動畫的時候,調(diào)換兩個視圖的位置,以達(dá)到視圖切換的效果
[parentView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
[UIView commitAnimations];
3、Block實(shí)現(xiàn)基礎(chǔ)動畫
1.淡入動畫
[UIView animateWithDuration:0.5 animations:^(void){
//動畫開始執(zhí)行調(diào)用的
self.myView.alpha = 1.0;
}completion:^(BOOL finish) {
NSLog(@"動畫結(jié)束");
}];
4、Block實(shí)現(xiàn)試圖切換的過渡動畫
UIViewAnimationOptions options = UIViewAnimationTransitionFlipFromRight;
[UIView transitionWithView:parentView duration:3.0f options:options animations:^{
[parentView exchangeSubviewAtIndex:page1Index withSubviewAtIndex:page2Index];
} completion:^(BOOL finished) {
NSLog(@"finished %d", finished);
}];