亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

iOS實現轉場動畫的3種方法示例

2020-07-26 02:21:05
字體:
來源:轉載
供稿:網友

什么是轉場動畫

在 NavigationController 里 push 或 pop 一個 View Controller,在 TabBarController 中切換到其他 View Controller,以 Modal 方式顯示另外一個 View Controller,這些都是 View Controller Transition。在 storyboard 里,每個 View Controller 是一個 Scene,View Controller Transition 便是從一個 Scene 轉換到另外一個 Scene, 中文稱呼其為「轉場」。 顧名思義,轉場動畫便是 View Controller Transition 過程中的動畫效果。

在 iOS 7 之前,我們只能使用系統提供的轉場效果,大部分時候夠用,但僅僅是夠用而已,總歸會有各種不如意的小地方,但我們卻無力改變;iOS 7 開放了相關 API 允許我們對轉場效果進行全面定制,這太棒了,自定義轉場動畫以及對交互手段的支持帶來了無限可能。

本文主要給大家介紹了關于iOS實現轉場動畫的3種方法,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧

1.CATransition

CATransition是CAAnimation的子類,用于過渡動畫或轉場動畫。為視圖層移入移除屏幕提供轉場動畫。首先來看一下簡單的Demo:

 CATransition *animation = [CATransition animation]; animation.type = kCATransitionFade; animation.subtype = kCATransitionFromRight; animation.duration = 1.0; // 在window上執行CATransition, 即可在ViewController轉場時執行動畫 [self.view.window.layer addAnimation:animation forKey:@"kTransitionAnimation"];  AViewController *vc = [[AViewController alloc] init]; [self presentViewController:vc animated:NO completion:nil];

將該動畫添加到window.layer上,則會present或push時使用指定的轉場動畫。

其中最主要的兩個屬性就是type和subtype。

  • type:轉場動畫的類型。

官方SDK只提供了四種轉場動畫的類型,即:

CA_EXTERN NSString * const kCATransitionFade;CA_EXTERN NSString * const kCATransitionMoveIn;CA_EXTERN NSString * const kCATransitionPush;CA_EXTERN NSString * const kCATransitionReveal;

私有的type:

NSString *const kCATransitionCube = @"cube"; NSString *const kCATransitionSuckEffect = @"suckEffect"; NSString *const kCATransitionOglFlip = @"oglFlip"; NSString *const kCATransitionRippleEffect = @"rippleEffect"; NSString *const kCATransitionPageCurl = @"pageCurl"; NSString *const kCATransitionPageUnCurl = @"pageUnCurl"; NSString *const kCATransitionCameraIrisHollowOpen = @"cameraIrisHollowOpen";NSString *const kCATransitionCameraIrisHollowClose = @"cameraIrisHollowClose";
  • subtype:動畫類型的方向
CA_EXTERN NSString * const kCATransitionFromRight;CA_EXTERN NSString * const kCATransitionFromLeft;CA_EXTERN NSString * const kCATransitionFromTop;CA_EXTERN NSString * const kCATransitionFromBottom;

上面講的是給window.layer添加transition,這樣使得在present或push時使用指定的轉場動畫。

既然講到這里了,就看一下把transition加在layer上。

看一下示例代碼:

- (void)viewDidLoad { [super viewDidLoad];  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];  [button setTitle:@"進入" forState:UIControlStateNormal];  button.backgroundColor = [UIColor redColor];  [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];  [self.view addSubview:button];  _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 150, 150)]; [self.view addSubview:_imageView]; _imageView.backgroundColor = [UIColor redColor]; _imageArray = @[[UIImage imageNamed:@"成果秀1"],[UIImage imageNamed:@"點贊他人1"],[UIImage imageNamed:@"偷師學藝1"],[UIImage imageNamed:@"學會欣賞3"]];  _imageView.image = [UIImage imageNamed:@"成果秀1"];}- (void)buttonClicked{  CATransition *animation = [CATransition animation]; animation.type = @"cube"; animation.subtype = kCATransitionFromRight; animation.duration = 1.0; //換圖片的時候使用轉場動畫 [self.imageView.layer addAnimation:animation forKey:nil]; //cycle to next image UIImage *currentImage = self.imageView.image; NSUInteger index = [self.imageArray indexOfObject:currentImage]; index = (index + 1) % [self.imageArray count]; self.imageView.image = self.imageArray[index]; }

2.transitionFromViewController

UIViewController自帶的方法:
transitionFromViewController:toViewController:duration:options:animations:completion:這個轉場動畫是用在當一個父視圖控制器中有幾個childViewController,當要在這幾個子視圖控制器之間切換時就可以用這個方法。

AViewController *a = self.childViewControllers[0];BViewController *b = self.childViewControllers[1];CViewController *c = self.childViewControllers[2];// Curl 翻頁效果// UIViewAnimationOptionTransitionCurlUp, UIViewAnimationOptionTransitionCurlDown// Flip 翻轉效果// UIViewAnimationOptionTransitionFlipFromLeft, UIViewAnimationOptionTransitionFlipFromRight// UIViewAnimationOptionTransitionFlipFromTop, UIViewAnimationOptionTransitionFlipFromDown[self transitionFromViewController:_currentViewController   toViewController:b    duration:0.5    options:UIViewAnimationOptionTransitionFlipFromRight   animations:^{} completion:^(BOOL finished) {}];

3.Transition Animation

1 UINavigationControllerDelegate + UIViewControllerAnimatedTransitioning

在UINavigationController的轉場動畫中,要指定UINavigationControllerDelegate對象:

self.navigationController.delegate = self;[self.navigationController pushViewController:itemVC animated:YES];

UINavigationControllerDelegate主要有以下兩個協議方法:

//pop- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController       interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0);//push- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController         animationControllerForOperation:(UINavigationControllerOperation)operation            fromViewController:(UIViewController *)fromVC             toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);

首先創建一個基類PDAnimatorBaseTransition實現UIViewControllerAnimatedTransitioning協議的方法。

UIViewControllerAnimatedTransitioning協議的方法有:

//動畫持續時間- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext;//轉場動畫實現細節- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;動畫結束時調用- (void)animationEnded:(BOOL) transitionCompleted;

PDAnimatorBaseTransition.h

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>typedef NS_ENUM(NSInteger, PDAnimationType){  animationTypePresent = 0, animationTypeDismiss , animationTypePush , animationTypePop,};@interface PDAnimatorBaseTransition : NSObject <UIViewControllerAnimatedTransitioning>@property (nonatomic, assign)PDAnimationType animationType;@property (nonatomic, strong)UIView *containerView;@property (nonatomic, strong)UIViewController *from;@property (nonatomic, strong)UIViewController *to;@property (nonatomic, strong)UIView *fromView;@property (nonatomic, strong)UIView *toView;@property (nonatomic, weak)id <UIViewControllerContextTransitioning> transitionContext;@end

PDAnimatorBaseTransition.m

#import "PDAnimatorBaseTransition.h"@interface PDAnimatorBaseTransition() @end@implementation PDAnimatorBaseTransition#pragma mark -required- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{  return 1.f;}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{  _transitionContext = transitionContext;  _containerView = [transitionContext containerView];  _from = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];  _to = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];  if([transitionContext respondsToSelector:@selector(viewForKey:)]){    _fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];  _toView = [transitionContext viewForKey:UITransitionContextToViewKey]; }else{    _fromView = _from.view;  _toView = _to.view; }  if(self.animationType == animationTypePresent){    [self animationPresent]; }else if (self.animationType == animationTypeDismiss){    [self animationDismiss]; }else if (self.animationType == animationTypePop){    [self animationPop]; }else{    [self animationPush]; } }#pragma mark -optional//動畫結束時回調- (void)animationEnded:(BOOL) transitionCompleted{ }- (void)animationPresent{ }- (void)animationDismiss{}- (void)animationPop{ }- (void)animationPush{ }

然后創建子類PDAnimatorPUshPopTransition繼承自PDAnimatorBaseTransition,實現- (void)animationPush,- (void)animationPop方法。

PDAnimatorPUshPopTransition.h

#import "PDAnimatorBaseTransition.h"#import <UIKit/UIKit.h>@interface PDAnimatorPUshPopTransition : PDAnimatorBaseTransition@property (nonatomic, assign)CGPoint itemCenter;@property (nonatomic, assign)CGSize itemSize;@property (nonatomic, strong)NSString *imageName;@end

PDAnimatorPUshPopTransition.m

#import "PDAnimatorPUshPopTransition.h"@implementation PDAnimatorPUshPopTransition- (instancetype)init{  self = [super init]; if(self){     }  return self;}- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{  return 5.f;}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{  [super animateTransition:transitionContext];}- (void)animationPush{  NSTimeInterval duration = [self transitionDuration:self.transitionContext]; __weak typeof(self) weakSelf = self;  self.containerView.backgroundColor = [UIColor lightGrayColor];  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 160)]; imageView.image = [UIImage imageNamed:self.imageName]; imageView.center = _itemCenter;  CGFloat initialScale = _itemSize.width / CGRectGetWidth(imageView.frame);  CGAffineTransform transform = CGAffineTransformIdentity;  transform = CGAffineTransformScale(transform, initialScale, initialScale); transform = CGAffineTransformRotate(transform, M_PI);  imageView.layer.affineTransform = transform;  // imageView.transform = CGAffineTransformMakeScale(initialScale, initialScale);  [self.containerView addSubview:imageView];   self.toView.frame = [self.transitionContext finalFrameForViewController:self.to]; CGPoint finalCenter = self.toView.center; self.toView.center = finalCenter; self.toView.alpha = 0.0; //這一句一定要 [self.containerView addSubview:self.toView];  [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{    imageView.layer.affineTransform = CGAffineTransformIdentity;    imageView.center = finalCenter;    self.fromView.alpha = 0.0;  self.containerView.backgroundColor = [UIColor redColor]; } completion:^(BOOL finished){    [imageView removeFromSuperview];    weakSelf.toView.alpha = 1.0f;  weakSelf.fromView.alpha = 1.0f;    [weakSelf.transitionContext completeTransition:![weakSelf.transitionContext transitionWasCancelled]]; }]; }- (void)animationPop{  NSTimeInterval duration = [self transitionDuration:self.transitionContext]; __weak typeof(self) weakSelf = self;  self.toView.frame = [self.transitionContext finalFrameForViewController:self.to];  [self.containerView insertSubview:self.toView belowSubview:self.fromView];  self.fromView.alpha = 0.0;  self.fromView.backgroundColor = [UIColor clearColor];  [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{    CGFloat initialScale = _itemSize.width / 200;  weakSelf.fromView.transform = CGAffineTransformMakeScale(initialScale, initialScale);  weakSelf.fromView.center = weakSelf.itemCenter;    weakSelf.toView.alpha = 1.0f; } completion:^(BOOL finished){    weakSelf.fromView.alpha = 0.0f;    [weakSelf.transitionContext completeTransition:![weakSelf.transitionContext transitionWasCancelled]]; }];}@end

然后我們在需要push的地方實現UINavigationControllerDelegate的協議方法:

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController           animationControllerForOperation:(UINavigationControllerOperation)operation               fromViewController:(UIViewController *)fromVC               toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0){  PDAnimatorPUshPopTransition *animationTransition = [[PDAnimatorPUshPopTransition alloc] init]; if(operation == UINavigationControllerOperationPush){    animationTransition.animationType = animationTypePush; }else if (operation == UINavigationControllerOperationPop){    animationTransition.animationType = animationTypePop; }  NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems]; if (indexPaths.count == 0) {  return nil; }  NSIndexPath *selectedIndexPath = indexPaths[0]; UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:selectedIndexPath];  // 一定要加上convertPoint:toView:操作 animationTransition.itemCenter = [self.collectionView convertPoint:cell.center toView:self.view]; animationTransition.itemSize = cell.frame.size; animationTransition.imageName = [NSString stringWithFormat:@"%ld", (long)selectedIndexPath.item];  return animationTransition;}

2 UIViewControllerTransitioningDelegate+UIViewControllerAnimatedTransitioning

首先需要設置被present的Controller的transitionDelegate

DemoViewControllerTransitionPresentedViewController *presentedVC = [[DemoViewControllerTransitionPresentedViewController alloc] init];presentedVC.transitionDelegate = self;[self presentViewController:presentedVC animated:YES completion:nil];

UIViewControllerTransitioningDelegate的代理的協議方法有:

//  prenent  - (id )animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source;//  pop  - (id )animationControllerForDismissedController:(UIViewController *)dismissed;//  prenent  - (id )interactionControllerForPresentation:(id )animator;//  pop  - (nullable id )interactionControllerForDismissal:(id )animator;

創建子類PDAnimationPresentTransitio繼承自基類PDAnimatorBaseTransition,實現- (void)animationPresent,- (void)animationDismiss方法。

PDAnimationPresentTransition.h

#import "PDAnimatorBaseTransition.h"@interface PDAnimationPresentTransition : PDAnimatorBaseTransition@end

PDAnimationPresentTransition.m

#import "PDAnimationPresentTransition.h"@implementation PDAnimationPresentTransition- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{  return 1.f;}- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{ [super animateTransition:transitionContext];}- (void)animationPresent{  NSTimeInterval duration = [self transitionDuration:self.transitionContext]; __weak typeof(self) weakSelf = self;  self.toView.frame = [self.transitionContext initialFrameForViewController:self.to];  self.fromView.frame = [self.transitionContext initialFrameForViewController:self.from];  CGAffineTransform transform = CGAffineTransformIdentity; transform = CGAffineTransformScale(transform, 0.001, 0.001); self.toView.layer.affineTransform = transform;  [self.containerView addSubview:self.toView];  self.toView.alpha = 0.0;  [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{    self.toView.layer.affineTransform = CGAffineTransformIdentity;  self.toView.frame = [self.transitionContext finalFrameForViewController:self.to];  self.toView.alpha = 1.0; } completion:^(BOOL finished){    BOOL wasCancelled = [weakSelf.transitionContext transitionWasCancelled];  [weakSelf.transitionContext completeTransition:!wasCancelled]; }]; }

然后我們在需要present的地方實現UIViewControllerTransitioningDelegate的代理方法。

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{  PDAnimationPresentTransition *animationTransition = [[PDAnimationPresentTransition alloc] init]; animationTransition.animationType = animationTypePresent; return animationTransition;}- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{  PDAnimationPresentTransition *animationTransition = [[PDAnimationPresentTransition alloc] init]; animationTransition.animationType = animationTypePresent; return animationTransition;}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩大片免费观看视频播放| 美女性感视频久久久| 91精品久久久久久久| 亚洲美女av在线| 欧美日韩中文字幕| 国产欧美一区二区三区在线看| 91精品国产自产在线老师啪| 日韩在线激情视频| 这里只有精品在线播放| 国产精品久久久久久久久男| 欧洲精品在线视频| 精品亚洲精品福利线在观看| 91av中文字幕| 亚洲xxxx18| 91人人爽人人爽人人精88v| 精品亚洲一区二区三区在线观看| 91精品国产高清久久久久久| 另类少妇人与禽zozz0性伦| 亚洲精品美女免费| 亚洲成人久久一区| 亚洲人成网站在线播| 黄色成人av网| 成人日韩av在线| 国产在线播放不卡| 国产97在线视频| 欧洲成人免费aa| 国产日韩中文字幕在线| 日韩一区二区久久久| 亚洲欧美国产va在线影院| 欧美中文字幕精品| 成人午夜一级二级三级| 亚洲第一视频网| 久久夜色撩人精品| 91精品久久久久久久久久| 精品日韩美女的视频高清| 欧美午夜精品久久久久久人妖| 久久久久国产精品免费| 国内精品久久久久影院优| 久久亚洲综合国产精品99麻豆精品福利| 国内精品久久久久影院 日本资源| 国产一区二区免费| 日韩av有码在线| 91av视频在线免费观看| 成人免费视频97| 美女久久久久久久久久久| 欧美日韩国产123| 国产在线精品一区免费香蕉| 国产97在线播放| 国产欧美日韩精品丝袜高跟鞋| 在线看欧美日韩| 国产精品6699| 久久精品国产亚洲精品| 国产欧美久久久久久| 色视频www在线播放国产成人| 精品欧美一区二区三区| 亚洲国产黄色片| 久久香蕉精品香蕉| 伊人久久男人天堂| 91av在线视频观看| 久久免费精品日本久久中文字幕| 日韩激情视频在线播放| 欧美日韩免费观看中文| 亚洲免费视频观看| 亚洲第一中文字幕| 亚洲欧美精品中文字幕在线| 久久久久久久久国产精品| 色综合伊人色综合网站| 青草热久免费精品视频| 久久久极品av| 欧美亚洲成人精品| 影音先锋欧美在线资源| 国产精品日日摸夜夜添夜夜av| 精品福利免费观看| 91在线免费观看网站| 2019最新中文字幕| 国产日产亚洲精品| 欧美大成色www永久网站婷| 午夜精品久久久久久99热| 欧美激情亚洲综合一区| 国产精品夜色7777狼人| 久久香蕉频线观| 欧美激情区在线播放| xx视频.9999.com| 国产精品揄拍500视频| 国产在线视频2019最新视频| 久久精品免费播放| 一本色道久久88亚洲综合88| 成人午夜高潮视频| 中文字幕日韩av综合精品| 91精品啪aⅴ在线观看国产| 日韩中文在线中文网在线观看| 最好看的2019年中文视频| 久久躁日日躁aaaaxxxx| 国产精品爽爽爽爽爽爽在线观看| 精品高清一区二区三区| 2019中文字幕在线免费观看| 国产ts人妖一区二区三区| 这里只有精品久久| 亚洲国产欧美日韩精品| 成人有码视频在线播放| 国产成人精品久久亚洲高清不卡| 久久九九国产精品怡红院| 超薄丝袜一区二区| 国产欧美一区二区三区久久人妖| 国模精品一区二区三区色天香| 午夜免费日韩视频| 亚洲肉体裸体xxxx137| 中文字幕久精品免费视频| 亚洲人免费视频| 亚洲美女视频网| 久久久亚洲国产天美传媒修理工| 中文字幕亚洲在线| 色婷婷综合久久久久中文字幕1| 欧美日韩一区二区免费在线观看| 亚洲xxxxx电影| 538国产精品一区二区免费视频| 成人a视频在线观看| 久久久精品国产网站| 成人在线视频网站| 中文字幕日韩av| 欧美中在线观看| 欧美性资源免费| 日韩欧美亚洲成人| 欧美在线激情网| 国产日韩欧美日韩大片| 92福利视频午夜1000合集在线观看| 国产亚洲精品91在线| 久久久电影免费观看完整版| 在线电影中文日韩| 秋霞av国产精品一区| 欧美一级片久久久久久久| 96精品久久久久中文字幕| 亚洲一区亚洲二区亚洲三区| 国产亚洲美女精品久久久| 奇米4444一区二区三区| 成人免费网站在线| 国产日韩综合一区二区性色av| 在线精品视频视频中文字幕| 欧美在线免费看| 亚洲精品电影网在线观看| 久热在线中文字幕色999舞| 亚洲国产精品久久| 亚洲人精品午夜在线观看| 久久99久久久久久久噜噜| 国产精品天天狠天天看| 欧美激情在线观看| 久久夜色精品亚洲噜噜国产mv| 国产精品国产三级国产专播精品人| 欧美日韩国产一区在线| 91高清视频在线免费观看| 一区二区亚洲精品国产| 在线成人免费网站| 福利二区91精品bt7086| 精品国产乱码久久久久久天美| 欧美成人免费网| 欧美大尺度电影在线观看| 精品久久久999| 亚洲国产美女精品久久久久∴| 夜夜嗨av色综合久久久综合网| 中文国产成人精品| 亚洲成av人片在线观看香蕉| 一区二区三区精品99久久| 国产69精品久久久| 久久国产一区二区三区|