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

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:21:59
字體:
來源:轉載
供稿:網友

什么是轉場動畫

在 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;}

總結

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


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日本成人激情视频| 色噜噜狠狠色综合网图区| 亚洲一区二区三区在线视频| 日韩精品视频免费在线观看| 亚洲国产精品久久久久| 成人欧美一区二区三区黑人孕妇| 国产精品久在线观看| 日韩成人在线电影网| 久久国内精品一国内精品| 国产精品十八以下禁看| 午夜精品一区二区三区在线视频| 国产精品爽爽爽| 亚洲丝袜av一区| 亚洲激情在线观看视频免费| 国产精国产精品| 九九精品视频在线观看| 97精品一区二区三区| 国产精品你懂得| 亚洲男人av在线| 91亚洲精品一区二区| 日韩中文字幕在线观看| 高清欧美性猛交xxxx黑人猛交| 国产精品一区二区性色av| 欧美精品久久久久久久久久| 午夜精品一区二区三区在线播放| 91精品国产91久久久| 日韩中文在线观看| 国产精品视频99| 日本免费久久高清视频| 九九视频这里只有精品| 久久久久久久国产| 亚洲精品视频在线观看视频| 欧美激情免费看| 久久精品国产免费观看| 欧美激情va永久在线播放| 欧美最近摘花xxxx摘花| 清纯唯美日韩制服另类| 日韩av在线导航| 午夜精品久久久久久久白皮肤| www.久久久久久.com| 亚洲影影院av| 国产精品麻豆va在线播放| 亚洲成人网av| 国产最新精品视频| 国产精品黄页免费高清在线观看| 国产成人亚洲综合青青| 日韩中文字幕免费视频| 欧洲s码亚洲m码精品一区| 亚洲自拍偷拍视频| 久久精品国产精品| 国产丝袜一区二区三区免费视频| 日韩福利伦理影院免费| 91av在线免费观看视频| 色综合久久中文字幕综合网小说| 久久精品人人做人人爽| 亚洲在线www| 国产成人免费av电影| 国产精品偷伦视频免费观看国产| 成人免费看吃奶视频网站| 在线中文字幕日韩| 日韩精品一区二区三区第95| 国产精品狼人色视频一区| 久久久久久av| 亚洲自拍偷拍网址| 欧美在线xxx| 91久久精品国产91久久性色| 欲色天天网综合久久| 91地址最新发布| 国内精品国产三级国产在线专| 亚洲奶大毛多的老太婆| 日韩视频一区在线| 欧美激情在线一区| 色吧影院999| 亚洲欧美日韩一区二区在线| 成人免费视频网| 国内精品久久久久影院优| 久久精品国产亚洲一区二区| 亚洲性线免费观看视频成熟| 日韩av在线免费播放| 国产成人精品免费久久久久| 国产精品久久99久久| 日韩电影大全免费观看2023年上| 国产日韩欧美夫妻视频在线观看| 91精品一区二区| 亚洲成人黄色在线| 久久成人人人人精品欧| 欧美激情一区二区三级高清视频| 综合激情国产一区| 亚洲综合国产精品| 日韩av在线免费观看| 成人网址在线观看| 色狠狠av一区二区三区香蕉蜜桃| 欧美精品在线网站| 亚洲一区二区久久久久久| 欧美壮男野外gaytube| 欧美精品在线极品| 久久国产精品久久久久久久久久| 日韩av电影在线免费播放| 久久久国产精品亚洲一区| 亚洲999一在线观看www| 91中文字幕一区| 欧美xxxx综合视频| 亚洲欧美日韩国产中文专区| 国产精品久久久久久久天堂| 日韩免费黄色av| 午夜免费日韩视频| 亚洲欧美激情另类校园| 蜜臀久久99精品久久久久久宅男| 欧美精品在线免费| 国产精品视频久久久久| 不卡在线观看电视剧完整版| 高跟丝袜欧美一区| 大伊人狠狠躁夜夜躁av一区| 亚洲国产精品热久久| 免费91麻豆精品国产自产在线观看| 国产精品第8页| 5566成人精品视频免费| 性欧美暴力猛交69hd| 理论片在线不卡免费观看| 亚洲一区二区免费| 国产欧美日韩免费看aⅴ视频| 夜夜嗨av色一区二区不卡| 人人做人人澡人人爽欧美| 亚洲精品国产精品久久清纯直播| 欧美小视频在线观看| 国产成人亚洲综合青青| 中文字幕精品一区二区精品| 91午夜理伦私人影院| 97免费视频在线播放| 亚洲精品午夜精品| 日韩美女视频中文字幕| 欧美多人乱p欧美4p久久| 国内自拍欧美激情| 一本大道香蕉久在线播放29| 久久久国产精品x99av| 国产午夜精品视频免费不卡69堂| 久久免费高清视频| 精品偷拍各种wc美女嘘嘘| 欧美激情喷水视频| 国产91色在线播放| 成人美女av在线直播| 欧美日韩亚洲一区二区三区| 中文字幕亚洲一区在线观看| 国产性色av一区二区| 激情成人在线视频| 性色av一区二区三区在线观看| 欧美激情中文字幕在线| 国产精品亚发布| 久久这里有精品| 一区二区三区黄色| 国产成人在线一区二区| 亚洲男人av电影| 欧美高清在线播放| 黄色一区二区在线观看| 欧美精品18videos性欧| 日韩在线视频免费观看高清中文| www.久久草.com| 在线视频精品一| 欧美日韩ab片| 亚洲白拍色综合图区| 久久国产精品网站| 欧美在线视频观看| 色午夜这里只有精品| 97在线免费观看|