一、ios動畫介紹ios
ios動畫實現有三種方式:ide
(1)、UIView動畫,最基本的動畫使用方式,經過改變UIView的屬性達到動畫效果(漸隱/漸現)動畫
(2)、CATransition動畫,用於兩個視圖過渡切換的動畫,系統內置了集中動畫效果,能夠直接使用url
(3)、CAAnimation動畫,ios核心動畫,結合繪圖能夠實現多變的動畫效果spa
二、UIView基本動畫代理
1 //標記動畫塊開始,設置動畫id和context 2 [UIView beginAnimations:@"testAnimation"context:@"test"]; 3 //以秒爲單位設置動畫持續時間 4 [UIView setAnimationDuration:0.5]; 5 //定義動畫加速和減速方式 6 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 7 //設置動畫代 8 [UIView setAnimationDelegate:self]; 9 //動畫結束後回調方法 10 [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context)]; 11 12 //視圖變化動做 13 CGRect frame = self.myView.frame; 14 frame.origin.y = 400; 15 self.myView.frame = frame; 16 //動畫結束 17 [UIView commitAnimations];
block使用動畫:code
[UIView animateWithDuration:0.7 animations:^{ [UIView setAnimationRepeatCount:1000]; self.myView.alpha = 0; } completion:^(BOOL finished){ if (finished) { } }];
動畫類型:orm
(1)、淡進淡出動畫: 設置view.alpha爲0.0 或1.0對象
(2)、位置變換動畫:更改view.frame實現blog
(3)、縮放動畫:
CGAffineTransform transform = view.transform; //設置動畫開始尺寸 view.transform = CGAffineTransform(transfrom,0.01,0.01); [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; //還原爲原來的尺寸 view.transform = CGAffineTransform(transfrom,0.01,0.01);
//此行代碼能夠代替上面的代碼,效果同樣
self.myView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
(4)、旋轉動畫:
view.transform = CGAffineTransformMakeRotation(3.14/2.0);
(5)、設置代理,動畫結束後調用相應的方法
[UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationStop)]; [UIVeiw commitAnimations];
//動畫結束後調用 - (void)animationStop { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; CGRect frame = self.myView.frame; frame.origin.y = 54; self.myView.frame = frame; [UIView commitAnimations]; }
(6)、視圖切換的過渡動畫
//注意動畫是做用在父視圖上的 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:parentView cache:YES]; [UIView commitAnimations]; //交換視圖0和視圖1的位置 [parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
(7)、自定義兩個控制器之間的切換動畫
[UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; //視圖切換動畫做用於導航控制器的view上 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:YES]; [UIView commitAnimations]; UIViewController *viewContr = [[UIViewController alloc] init]; [self.navigationController pushViewController:viewContr animated:NO];
(8)、過渡動畫的block使用方法
[UIView transitionWithView:self.parentView duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ [self.parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; } completion:NULL];
options類型:
二、CATransition動畫的使用
CATransition是對UIView進行更低層次的繪製,做用於UIView的Layer層,主要用於兩個視圖切換過渡時的動畫
(2.1)、導航控制器切換動畫
CATransition *animation = [CATransition animation]; animation.duration = 0.6; animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"]; //動畫類型 animation.type = kCATransitionPush; //動畫子類型 animation.subtype = kCATransitionFromLeft; [self.navigationController.view.layer addAnimation:animation forKey:@"test"]; UIViewController *viewCtrl = [[UIViewController alloc] init]; [self.navigationController pushViewController:viewCtrl animated:NO];
動畫類型:
三、UIImageView播放連續動畫
imageView.animationImages = images; imageView.animationDuration = 0.5f; [imageView startAnimating];
四、開源動畫庫HMGLTransitions的使用
基於openGL封裝的開源動畫庫,使用時必須導入QuartzCore.framework和OpenGLES.framework
動畫效果繼承於HMGLTransition:
五種動畫效果:
//開門效果 DoorsTransition *animation = [[DoorsTransition alloc] init]; animation.transitionType = DoorsTransitionTypeClose; //3D效果 Switch3DTransition *switch3D = [[Switch3DTransition alloc] init]; switch3D.transitionType = Switch3DTransitionLeft; // ClothTransition *cloth = [[ClothTransition alloc] init]; //翻頁效果 FlipTransition *flip = [[FlipTransition alloc] init]; flip.transitionType = FlipTransitionLeft; //上下翻轉效果 RotateTransition *rotate = [[RotateTransition alloc] init];
(1)、視圖切換
//建立動畫類型對象 Switch3DTransition *animation = [[Switch3DTransition alloc] init]; //設置動畫類型對象 [[HMGLTransitionManager sharedTransitionManager] setTransition:animation]; //設置動畫須要添加的視圖 [[HMGLTransitionManager sharedTransitionManager] beginTransition:parentView]; [[HMGLTransitionManager sharedTransitionManager] commitTransition]; //切換視圖 [parentVIew exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
(2)、模態視圖彈出動畫效果
//定義動畫對象
DoorsTransition *animation = [[DoorsTransition alloc] init]; [[HMGLTransitionManager sharedTransitionManager] setTransition:animation]; ModalViewController *viewCtrl = [[ModalViewController alloc] init]; //打開模態視圖 [[HMGLTransitionManager sharedTransitionManager] presentModalViewController:viewCtrl onViewController:self];
(3)、模態視圖關閉的動畫效果:
[[HMGLTransitionManager sharedTransitionManager] setTransition:animation];
[[HMGLTransitionManager sharedTransitionManager] dismissModalViewController:self];