利用转场动画来实现类似QQ语音通话界面隐藏和出现的效果

浮动可拖拽按钮功能的实现可以参考前一篇文章。利用手势拖拽控件并设置拖拽范围.
接下来利用转场动画来实现类似QQ语音通话界面隐藏和出现的转场动画效果。
第一步,建立一个继承于NSObject的类,实现UIViewControllerAnimatedTransitioning协议,然后利用贝塞尔曲线来绘制界面出现和消失的动画路径。代码如下:

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

typedef NS_ENUM(NSUInteger ,ZTransitionType)
{
    ZTransitionTypeShow = 0,
    ZTransitionTypeDismiss
};
@interface ZTransition : NSObject<UIViewControllerAnimatedTransitioning,CAAnimationDelegate>

/*
 *转场动画操作type
 */
@property (assign,nonatomic)ZTransitionType transitionType;


+ (instancetype)transitionWithType:(ZTransitionType)transitionType;

- (instancetype)initWithTransitionType:(ZTransitionType)transitionType;

@end

#import "ZTransition.h"
#import "ViewController.h"

#define Animaton_Time 0.5f
@implementation ZTransition

+ (instancetype)transitionWithType:(ZTransitionType)transitionType
{
    return [[self alloc]initWithTransitionType:transitionType];
}

- (instancetype)initWithTransitionType:(ZTransitionType)transitionType
{
    self = [super init];
    if (self)
    {
        self.transitionType = transitionType;
    }
    return self;
}


#pragma mark 转场动画时长
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return Animaton_Time;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    switch (self.transitionType)
    {
        case ZTransitionTypeShow:
        {
            [self showAnimaton:transitionContext];
        }
            break;
        case ZTransitionTypeDismiss:
        {
            [self dismissAnimation:transitionContext];
        }
            break;
        default:
            break;
    }
}

#pragma mark -- show
- (void)showAnimaton:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UINavigationController * fromVC = (UINavigationController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    ViewController * temVC = fromVC.viewControllers.lastObject;
    UIView * containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];

    //TODO:画两个圆
    UIBezierPath *startRound = [UIBezierPath bezierPathWithOvalInRect:temVC.roundFrame];
    CGFloat x = MAX(temVC.roundFrame.origin.x, containerView.frame.size.width - temVC.roundFrame.origin.x);
    CGFloat y = MAX(temVC.roundFrame.origin.y, containerView.frame.size.height - temVC.roundFrame.origin.y);
    CGFloat radius = sqrtf(pow(x, 2) + pow(y, 2));
    UIBezierPath *endRound = [UIBezierPath bezierPathWithArcCenter:containerView.center
                                                            radius:radius
                                                        startAngle:0
                                                          endAngle:M_PI * 2
                                                         clockwise:YES];
    //TODO:创建CAShpeLayer遮罩层
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = endRound.CGPath;
    toVC.view.layer.mask = maskLayer;

    //TODO:动画路径
    CABasicAnimation *maskLayerAni = [CABasicAnimation animationWithKeyPath:@"path"];
    maskLayerAni.delegate = self;
    maskLayerAni.fromValue = (__bridge id _Nullable)(startRound.CGPath);
    maskLayerAni.toValue = (__bridge id _Nullable)(endRound.CGPath);
    maskLayerAni.duration = [self transitionDuration:transitionContext];
    maskLayerAni.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [maskLayerAni setValue:transitionContext forKey:@"transitionContext"];
    [maskLayer addAnimation:maskLayerAni forKey:@"path"];

}

#pragma mark ---dismiss
- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UINavigationController *toVC = (UINavigationController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    ViewController *tempVC = toVC.viewControllers.lastObject;
    UIView *containerView = [transitionContext containerView];

    //TODO:画两个圆
    CGFloat radius = sqrtf(containerView.frame.size.height * containerView.frame.size.height + containerView.frame.size.width * containerView.frame.size.width)/2;
    UIBezierPath *startRoundPath = [UIBezierPath bezierPathWithArcCenter:containerView.center
                                                                  radius:radius
                                                              startAngle:0
                                                                endAngle:M_PI * 2
                                                               clockwise:YES];
    UIBezierPath *endRoundPath = [UIBezierPath bezierPathWithOvalInRect:tempVC.roundFrame];

    //TODO:创建遮罩层
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillColor = [UIColor colorWithRed:32/255 green:62/255 blue:85/255 alpha:1.0].CGColor;
    maskLayer.path = endRoundPath.CGPath;
    fromVC.view.layer.mask = maskLayer;

    //TODO:创建转场动画路径
    CABasicAnimation *maskAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    maskAnimation.delegate = self;
    maskAnimation.fromValue = (__bridge id _Nullable)(startRoundPath.CGPath);
    maskAnimation.toValue = (__bridge id _Nullable)(endRoundPath.CGPath);
    maskAnimation.duration = [self transitionDuration:transitionContext];
    maskAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [maskAnimation setValue:transitionContext forKey:@"transitionContext"];
    [maskLayer addAnimation:maskAnimation forKey:@"path"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    switch (self.transitionType)
    {
        case ZTransitionTypeShow:
        {
            id<UIViewControllerContextTransitioning>transitionContext = [anim valueForKey:@"transitionContext"];
            [transitionContext completeTransition:YES];
        }
            break;
        case ZTransitionTypeDismiss:
        {
            id<UIViewControllerContextTransitioning>transitionContext = [anim valueForKey:@"transitionContext"];
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
            if ([transitionContext transitionWasCancelled])
            {
                [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;;
            }
        }
            break;
        default:
            break;
    }
}

第二步:在通话界面实现相应的动画操作。
代码如下:

#import "TelephoneViewController.h"
#import "ViewController.h"
#import "ZTransition.h"
@interface TelephoneViewController ()<UIViewControllerTransitioningDelegate>

@end

@implementation TelephoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Dismiss");


    UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    [self.view addSubview:tools];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.transitioningDelegate = self;
        self.modalTransitionStyle = UIModalPresentationCustom;
    }
    return self;
}

- (IBAction)dismissVC:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [ZTransition transitionWithType:ZTransitionTypeShow];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return [ZTransition transitionWithType:ZTransitionTypeDismiss];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

第三部分,最后效果图

QQ20170612-161637.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标...
    VincentHK阅读 5,326评论 3 44
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,982评论 4 60
  • 电视剧里的情节反反复复 无论多久说好的朝朝暮暮 他们说爱过的人最怕孤独 也曾走在冬夜里寒风刺骨 祈祷你...
    陈北笙阅读 170评论 0 0
  • 光说说今天要去给父亲买衣服叠元宝这件事情,世界真是太复杂了,太丰富了,把我的大脑冲的太大了,我自己。需要静一静,因...
    马上做阅读 131评论 0 0
  • 睡皮,不知什么时候开始对这个我心中永远的21号有了这样一个看似讽刺的称号。 这不是我的原创,也记不清是从何而来,因...
    大宝粑粑没有腰阅读 174评论 0 0