自定義Push和Pop的動(dòng)畫(huà)效果

先寫(xiě)一些通用部分

實(shí)現(xiàn)協(xié)議

在要添加自定義效果的視圖控制器中實(shí)現(xiàn)代理協(xié)議UINavigationControllerDelegate

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.navigationController.delegate = self;
}

實(shí)現(xiàn)代理方法

#pragma mark -UINavigationControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPush) {
        //返回我們自定義的效果
        return [[PushTransition alloc]init];
    }
    else if (operation == UINavigationControllerOperationPop){
        return [[PopTransition alloc]init];
    }
    //返回nil則使用默認(rèn)的動(dòng)畫(huà)效果
    return nil;
}

注:PushTransition拙绊、PopTransition為具體效果的實(shí)現(xiàn)

PushTransition的實(shí)現(xiàn)

這種效果的實(shí)現(xiàn)類需要實(shí)現(xiàn)UIViewControllerAnimatedTransitioning協(xié)議

通用代碼部分

//.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface PushTransition : NSObject<UIViewControllerAnimatedTransitioning>

@end

//.m
#import "PushTransition.h"

@interface PushTransition ()

@end

@implementation PushTransition

// This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    //返回動(dòng)畫(huà)的執(zhí)行時(shí)間
    return 1.0f;
}
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{    
    self.transitionContext = transitionContext;

    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    //不添加的話唯欣,屏幕什么都沒(méi)有
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:fromVC.view];
    [containerView addSubview:toVC.view];

    //注意注意一定要注意這里熊经。我們就是在這里添加我們所需要的動(dòng)畫(huà)效果。將動(dòng)畫(huà)效果的代碼放在這里就行了圆存。重要的事情要說(shuō)好幾遍

    //如果取消了就設(shè)置為NO仪糖,反之找前,設(shè)置為YES栏妖。如果添加了動(dòng)畫(huà),這句代碼在動(dòng)畫(huà)結(jié)束之后再調(diào)用
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}

實(shí)例程序一

實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的扇形動(dòng)畫(huà)

//.m
#import "PushTransition.h"

@interface PushTransition ()

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

@end

@implementation PushTransition

// This is used for percent driven interactive transitions, as well as for container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    return 1.0f;
}
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    self.transitionContext = transitionContext;

    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    //不添加的話辜窑,屏幕什么都沒(méi)有
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:fromVC.view];
    [containerView addSubview:toVC.view];    
CGRect originRect = CGRectMake(0, 0, 50, 50);

    UIBezierPath *maskStartPath = [UIBezierPath bezierPathWithOvalInRect:originRect];
    UIBezierPath *maskEndPath = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(originRect, -2000, -2000)];

    //創(chuàng)建一個(gè)CAShapeLayer來(lái)負(fù)責(zé)展示圓形遮蓋
CAShapeLayer *maskLayer = [CAShapeLayer layer];
//    maskLayer.path = maskEndPath.CGPath;//將他的path指定為最終的path钩述,來(lái)避免在動(dòng)畫(huà)完成后回彈
toVC.view.layer.mask = maskLayer;

    CABasicAnimation *maskAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskAnimation.fromValue = (id)maskStartPath.CGPath;
    maskAnimation.toValue = (id)maskEndPath.CGPath;
    maskAnimation.duration = [self     transitionDuration:transitionContext];
    maskAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    maskAnimation.fillMode = kCAFillModeForwards;
    maskAnimation.removedOnCompletion = NO;
    maskAnimation.delegate = self;
    [maskLayer addAnimation:maskAnimation forKey:@"Path"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"搞定結(jié)束");
    [self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
    //去除mask
    [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
    [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
}

@end

實(shí)例程序二

實(shí)現(xiàn)從下方彈出的效果

NSTimeInterval duration = [self transitionDuration:transitionContext];
CGRect screenBounds = [[UIScreen mainScreen]bounds];
CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
toVC.view.frame = CGRectOffset(finalFrame, 0, screenBounds.size.height);

[UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:0.6 initialSpringVelocity:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
    toVC.view.frame = finalFrame;
} completion:^(BOOL finished) {
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市穆碎,隨后出現(xiàn)的幾起案子牙勘,更是在濱河造成了極大的恐慌,老刑警劉巖惨远,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谜悟,死亡現(xiàn)場(chǎng)離奇詭異话肖,居然都是意外死亡北秽,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門最筒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)贺氓,“玉大人,你說(shuō)我怎么就攤上這事床蜘≌夼啵” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵邢锯,是天一觀的道長(zhǎng)扬蕊。 經(jīng)常有香客問(wèn)我,道長(zhǎng)丹擎,這世上最難降的妖魔是什么尾抑? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮蒂培,結(jié)果婚禮上再愈,老公的妹妹穿的比我還像新娘。我一直安慰自己护戳,他們只是感情好翎冲,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著媳荒,像睡著了一般抗悍。 火紅的嫁衣襯著肌膚如雪驹饺。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,554評(píng)論 1 305
  • 那天檐春,我揣著相機(jī)與錄音逻淌,去河邊找鬼。 笑死疟暖,一個(gè)胖子當(dāng)著我的面吹牛卡儒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播俐巴,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼骨望,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了欣舵?” 一聲冷哼從身側(cè)響起擎鸠,我...
    開(kāi)封第一講書(shū)人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎缘圈,沒(méi)想到半個(gè)月后劣光,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡糟把,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年绢涡,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遣疯。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡雄可,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出缠犀,到底是詐尸還是另有隱情数苫,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布辨液,位于F島的核電站虐急,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏滔迈。R本人自食惡果不足惜止吁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望亡鼠。 院中可真熱鬧赏殃,春花似錦、人聲如沸间涵。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至抗蠢,卻和暖如春举哟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背迅矛。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工妨猩, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人秽褒。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓壶硅,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親销斟。 傳聞我的和親對(duì)象是個(gè)殘疾皇子庐椒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容