• 自己定义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
    

    这种话,我们外面用起来就非常easy了:

    #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的自己定义动画效果了。

  • 相关阅读:
    洛谷 P2678 跳石头
    洛谷 P1145 约瑟夫
    LibreOJ #515. 「LibreOJ β Round #2」贪心只能过样例
    洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths
    网络编程 --- TCP
    进程
    并发编程
    网络编程 --- UDP
    网络编程
    面向对象编程 --- 反射
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7276786.html
Copyright © 2020-2023  润新知