自定义modal动画

在很多场景中,我们都需要实现各种动画,这回我们来尝试搞一下控制器间跳转的modal动画。

 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    ZYSecondViewController *second = [[ZYSecondViewController alloc]init];

    second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    [self presentViewController:second animated:YES completion:nil];

}
  • 上面是系统提供的动画的样式,但是系统提供的动画有时候满足不了我们的需求,所以我们就要自定义动画了,我们接下来,就重点的来说一下自定义动画这一块的内容。
  • 准备工作:我之前写过的单例:一行代码搞定单例
  • 以及一些坐标的扩展,这里我们直接调用一下:
  • ZYtransition.h(单例)
#import "Singleton.h"
@interface ZYtransition : NSObject <UIViewControllerTransitioningDelegate>
SingletonH(transition)
@end
  • ZYtransition.m
#import "ZYtransition.h"
#import "ZYAnimatedTransitioning.h"
#import "ZYPresentationController.h"
@implementation ZYtransition
SingletonM(transition)

 - (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{

    ZYPresentationController *pc = [[ZYPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
    return pc;
}
 - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    ZYAnimatedTransitioning *anim = [[ZYAnimatedTransitioning alloc]init];
    anim.presented = YES;
    return anim;
}

 - (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    ZYAnimatedTransitioning *anim = [[ZYAnimatedTransitioning alloc]init];
    anim.presented = NO;
    return anim;
}
  • ZYAnimatedTransitioning.h (负责动画)
@interface ZYAnimatedTransitioning : NSObject<UIViewControllerAnimatedTransitioning>
@property(nonatomic,assign)BOOL presented;
@end
  • ZYAnimatedTransitioning.m
const NSTimeInterval duraton  = 0.5;
@implementation ZYAnimatedTransitioning
 - (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    return  duraton;
}
 - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    //   UITransitionContextToViewKey
    //   UITransitionContextFromViewKey

    if (self.presented)
    {
        UIView *toView =  [transitionContext viewForKey:UITransitionContextToViewKey];

        toView.y = -toView.height;

        [UIView animateWithDuration:duraton animations:^{
            toView.y = 0;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];

        }];
    }else
    {
        UIView *fromView =  [transitionContext viewForKey:UITransitionContextFromViewKey];

        [UIView animateWithDuration:duraton animations:^{
            fromView.y = -fromView.height;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];

        }];
    }
}
@end
  • 搞一个ZYPresentationController

    .h

@interface ZYPresentationController : UIPresentationController

.m

@implementation ZYPresentationController

- (void)presentationTransitionWillBegin
{

    self.presentedView.frame = self.containerView.bounds;
    [self.containerView addSubview:self.presentedView];
}
- (void)presentationTransitionDidEnd:(BOOL)completed
{
//    NSLog(@"%s",__func__);
}
- (void)dismissalTransitionWillBegin
{
//    NSLog(@"%s",__func__);
}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
    [self.presentedView removeFromSuperview];
}
@end


这样的话,我们外面用起来就非常简单了:

#import "ViewController.h"
#import "ZYSecondViewController.h"
#import "ZYtransition.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    ZYSecondViewController *second = [[ZYSecondViewController alloc]init];

    second.modalPresentationStyle = UIModalPresentationCustom;

    second.transitioningDelegate = [ZYtransition sharedtransition];

    [self presentViewController:second animated:YES completion:nil];

}

@end

外面只需要跟平时一样,然后设置显示的样式为自定义,然后制定代理,就能实现modal的自定义动画效果了。

时间: 2024-11-05 03:34:25

自定义modal动画的相关文章

iOS:自定义模态动画 --UIPresentationController

UIPresentationController :展示控制器,是iOS8的一个新特性,用来展示模态窗口的.它是所有模态控制器的管理者. 即: 1> 管理所有Modal出来的控制器 2> 管理所有通过- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion方法显示出来的控制器 3> 

自定义ModalViewController 动画效果

iOS 7 自定义ViewController动画切换 自定义动画切换的相关的主要API 在深入之前,我们先来看看新SDK中有关这部分内容的相关接口以及它们的关系和典型用法.这几个接口和类的名字都比较相似,但是还是能比较好的描述出各自的职能的,一开始的话可能比较迷惑,但是当自己动手实现一两个例子之后,它们之间的关系就会逐渐明晰起来.(相关的内容都定义在UIKit的UIViewControllerTransitioning.h中了) @protocol UIViewControllerContex

iOS 封装Modal动画代码

1.自定义转场动画要写的代码很多,如果整个项目的转场动画都必须一致,则必须考虑把modal代码封装起来 secondVC *second = [[secondVC alloc] init]; second.modalPresentationStyle = UIModalPresentationCustom; second.transitioningDelegate = 自定义一个代理; [self presentViewController:second animated:YES complet

Android开发之自定义View-可动画展开收缩View的实现

有时候需要点击一个view可以动画展开和收缩折叠一个View这样的效果,这样就可以直接自定义View来实现. 本例中,采用继承FrameLayout来实现自定义的ExpandView.下面将详细介绍各个部分来实现该类以及如何使用该自定义视图. 效果图如下: 未展开效果: 正在向上折叠收缩中的效果: 已经展开效果: 自定义展开类:ExpandView的实现: package com.czm.customview; import android.content.Context; import and

自己定义modal动画

在非常多场景中.我们都须要实现各种动画.这回我们来尝试搞一下控制器间跳转的modal动画. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { ZYSecondViewController *second = [[ZYSecondViewController alloc]init]; second.modalTransitionStyle = UIModalTransitionStyleFl

Android自定义Transition动画

本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发. 曾经(或者现在)很多人说起Android和iOS都会拿Android的UI设计来开黑, "你看看人家iOS的设计, 再来看看Android的, 差距怎么就这么大呢?", 对于这种说辞, 可以一句话来总结一下"他们还停留在4.X之前的时代". 自从Android5.0推出Material Design设计规范后, Android在设计上早已甩那个万年不变的iOS好几十条街! 以上纯

iOS_20_微博自定义可动画切换的导航控制器

最终效果: AnimatedNavigationController.h // // AnimatedNavigationController.h // 20_帅哥no微博 // // Created by beyond on 14-8-10. // Copyright (c) 2014年 com.beyond. All rights reserved. // 继承自导航控制器,但是多了一个功能,可以监听手势,进行动画切换 #import <UIKit/UIKit.h> @interface

视图自定义旋转动画 类似百度音乐

@interface FirstViewController () @property (nonatomic,assign)BOOL isplay; @end @implementation FirstViewController @synthesize isplay; - (void)viewDidLoad { [super viewDidLoad]; isplay = NO; //圆角 self.imgview.layer.cornerRadius = 150.0 / 2.0; self.i

自定义简单动画

注释: duration 动画完成时间, delay 动画延长时间,可以理解为动画速度的时间控制 一,利用 CGAffineTransformMakeScale来做图形的缩放效果 view.transform = CGAffineTransformMakeScale(1, 1); [UIView animateKeyframesWithDuration:duration/3 delay:delay options:0 animations:^{ // End view.transform = C