IOS轉(zhuǎn)場動畫

最近項目做完了,就進(jìn)入無休止的修改中了。也順便在看swift。學(xué)到了兩個轉(zhuǎn)場動畫,也給大家看看
PS:其實我一直以為轉(zhuǎn)場動畫又難又不實用,哈哈

轉(zhuǎn)場動畫1: (先看效果圖)

XXX.gif

這種轉(zhuǎn)場方式跟boss直聘的應(yīng)該差不多,所有就收集了

屏幕快照 2017-01-09 下午4.52.28.png

先說說建的類的用處吧: ViewController:第一個控制器 BViewController:push出來的第二個控制器 PushAnimateion:繼承NSObject的工具類,用于修改push動畫 PopAnimateion:繼承NSObject的工具類,用于修改Pop動畫
下面給出ViewController里面代碼和PushAnimateion里面具體代碼,另外兩個類似,只上截圖。另外,ViewController的導(dǎo)航欄是在storyboard里面直接加的 ViewController .m:

#import "ViewController.h"
#import "BViewController.h"
#import "PushAnimateion.h"

@interface ViewController ()<UINavigationControllerDelegate>

@end

@implementation ViewController

//這個控制器的代理一定要在viewWillAppear里面設(shè)置
//因為每次push的時候控制器不會dealloc
//所以如果寫在viewDidLoad里面的話在pop回來的時候就不會再次執(zhí)行代理,動畫就會失效
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //設(shè)置背景圖片
    self.view.layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"4"].CGImage);
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    BViewController *view = [BViewController new];
    [self.navigationController pushViewController:view animated:YES];
}

//需要返回的是一個id類型的且遵循UIViewControllerAnimatedTransitioning協(xié)議的
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC {
    //根據(jù)類型返回對應(yīng)動畫
    if (operation == UINavigationControllerOperationPush) {
        return [PushAnimateion new];
    }else {
        return nil;
    }
}

@end

PushAnimateion里面代碼:

//這是.h里面的,要遵循這個協(xié)議,不然UINavigationControllerDelegate返回這個類的對象會報錯,所以寫在.h里面
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface PushAnimateion : NSObject<UIViewControllerAnimatedTransitioning>

@end

//.m里面代碼
#import "PushAnimateion.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface PushAnimateion ()<CAAnimationDelegate>

@property (nonatomic, retain) id<UIViewControllerContextTransitioning> transitionContext;

@end

@implementation PushAnimateion

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return .5;
    
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    self.transitionContext = transitionContext;
    
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    [[transitionContext containerView] addSubview:fromVC.view];
    [[transitionContext containerView] addSubview:toVC.view];
    
    UIBezierPath *starPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, HEIGHT*0.5, WIDTH, 1)];
    UIBezierPath *endPath  = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, WIDTH, HEIGHT)];
    
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.path = endPath.CGPath;
    toVC.view.layer.mask = maskLayer;
    
    CABasicAnimation *animate = [CABasicAnimation animationWithKeyPath:@"path"];
    animate.fromValue = (__bridge id _Nullable)(starPath.CGPath);
    animate.toValue = (__bridge id _Nullable)(endPath.CGPath);
    animate.duration = [self transitionDuration:transitionContext];
    animate.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animate.delegate = self;
    [maskLayer addAnimation:animate forKey:@"path"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
    
    [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
    [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
}

@end

如果建的類名相同,可以直接把代碼拖到項目里,代碼都拷貝齊全的 下面給兩張圖片是BViewController的m文件和PopAnimateion的m文件。PopAnimateion的h文件里跟PushAnimateion里面是一樣的,代碼基本一樣,可以復(fù)制過去直接用

屏幕快照 2017-01-09 下午5.05.36.png

屏幕快照 2017-01-09 下午5.05.50.png

該項目有一點(diǎn)點(diǎn)小bug,就是pop回來的時候會閃一下,求大神解決了告知,??

轉(zhuǎn)場動畫2:

xx.gif

這個效果是系統(tǒng)自帶的動畫,做的比較粗糙,需要使用的朋友可以自己再修復(fù)一下 這個比較簡單,直接上代碼然后說一下就好了

rootViewController的.m里面代碼

#import "ViewController.h"
#import "AViewController.h"
#import "BViewController.h"

@interface ViewController ()

@property (nonatomic, assign) NSInteger currentChildNumber;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentChildNumber = 0;
    [self addChildViewController:[AViewController new]];
    [self addChildViewController:[BViewController new]];
    
    [self.view addSubview:self.childViewControllers.firstObject.view];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification) name:@"push" object:nil];
}

- (void)getNotification {
    [self transitionFromViewController:self.currentChildNumber == 0 ? self.childViewControllers.firstObject : self.childViewControllers.lastObject
                      toViewController:self.currentChildNumber == 1 ? self.childViewControllers.firstObject : self.childViewControllers.lastObject
                              duration:1
                               options:self.currentChildNumber == 0 ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight
                            animations:nil
                            completion:nil];
    self.currentChildNumber = (self.currentChildNumber + 1) % 2;
}

@end

只需要另外新建兩個控制器,在touchBegin里面發(fā)個通知,這里接收就OK

需要代碼的可以加群:515385179(群里都沒有人,來點(diǎn)大神救救我吧??)
我是小白,有沒有收徒的,我報名??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一如既往的在看文章之前我們先來看一下Demo的效果: 要實現(xiàn)這樣的效果需要哪些技術(shù)呢?CAShaperLayer?...
    ty_Chen閱讀 7,332評論 3 19
  • 前言的前言 唐巧前輩在微信公眾號「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各項指標(biāo)...
    VincentHK閱讀 5,581評論 3 44
  • 轉(zhuǎn)場動畫學(xué)習(xí)中...TransitionDemo代碼 實現(xiàn)自定義的轉(zhuǎn)場動畫(只涉及自定義動畫,不管手勢驅(qū)動) 涉及...
    YaoYaoX閱讀 742評論 0 3
  • 轉(zhuǎn)場動畫分為兩種 導(dǎo)航欄轉(zhuǎn)場使用模態(tài)轉(zhuǎn)場 轉(zhuǎn)場動畫就是一個控制器切換到另一個控制器之間的過程,在這個過程中獲取到兩...
    會武的鋤頭閱讀 1,126評論 0 4
  • 艷萍給自己起了個QQ昵稱叫"快樂".可是據(jù)她自己說現(xiàn)在并不快樂.記憶中的她,卻是快樂的. 記得高中...
    _荷包蛋_閱讀 394評論 0 0

友情鏈接更多精彩內(nèi)容