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

UIPresentationController :展示控制器,是iOS8的一个新特性,用来展示模态窗口的。它是所有模态控制器的管理者。

即:

1> 管理所有Modal出来的控制器

2> 管理所有通过- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion方法显示出来的控制器

3> 管理\监听切换控制器的过程

4> presentingViewController:后面的控制器

5> presentedViewController:前面的控制器

6> presentedView:前面的控制器的view

注意:

1.只要调用了[self presentViewController: animated: completion:]方法

2.首先会创建一个UIPresentationController

3.然后由UIPresentationController管理控制器的切换

拓展:

1、系统给定的集中模态动画展示样式modalPresentationStyle有如下几种:

typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {

UIModalPresentationFullScreen = 0,

UIModalPresentationPageSheet NS_ENUM_AVAILABLE_IOS(3_2),

UIModalPresentationFormSheet NS_ENUM_AVAILABLE_IOS(3_2),

UIModalPresentationCurrentContext NS_ENUM_AVAILABLE_IOS(3_2),

UIModalPresentationCustom NS_ENUM_AVAILABLE_IOS(7_0),

UIModalPresentationOverFullScreen NS_ENUM_AVAILABLE_IOS(8_0),

UIModalPresentationOverCurrentContext NS_ENUM_AVAILABLE_IOS(8_0),

UIModalPresentationPopover NS_ENUM_AVAILABLE_IOS(8_0),

UIModalPresentationNone NS_ENUM_AVAILABLE_IOS(7_0) = -1,

};

2、系统给定的集中模态动画过渡样式modalTransstionStyle有如下几种:

typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {

UIModalTransitionStyleCoverVertical = 0,

UIModalTransitionStyleFlipHorizontal,

UIModalTransitionStyleCrossDissolve,

UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),

};

由于给定的东西毕竟有限,有的时候不能满足自己需要的动画效果,此时我们可以自己自定义modal动画,做出炫目的动画。

过程:其实做自定义的modal动画还是比较复杂的,因为我既需要自定义动画展示样式类,也需要自定义动画过渡样式类,并且要实现这两个类对应的协议,设置过渡代理,然后实现协议方法。

下面就是具体的实例,自定义modal动画:

导入必要的第三方源文件,方便后面代码的复用

在故事板中设置ViewController控制器的视图颜色

创建一个secondViewController.h/.m/.xib文件,设置视图颜色

创建自定义的动画展示样式类CustomPresentationController.h/.m文件,直接继承自UIPresentationController

.h文件:

#import <UIKit/UIKit.h>

@interface CustomPresentationController : UIPresentationController

@end

.m文件:重写下面的几个方法

#import "CustomPresentationController.h"

@implementation CustomPresentationController

//可以改变被模态的控制器视图的尺寸
//- (CGRect)frameOfPresentedViewInContainerView
//{
//
//    /**  containerView是容纳presentedView的一个容器  */
//    //return CGRectMake(0,50,self.containerView.frame.size.width,self.containerView.frame.size.height-100);
//
//    return CGRectInset(self.containerView.bounds, 0, 50);
//}

//过渡即将开始时的处理
- (void)presentationTransitionWillBegin
{
    self.presentedView.frame = self.containerView.frame;
    [self.containerView addSubview:self.presentedView];
}

- (void)presentationTransitionDidEnd:(BOOL)completed
{

}
- (void)dismissalTransitionWillBegin
{

}

//过渡消失时的处理
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
    [self.presentedView removeFromSuperview];
}
@end

创建自定义的动画过渡样式类CustomAnimationTransition.h/.m文件,实现UIViewControllerAnimatedTransitioning协议

.h文件:

#import <UIKit/UIKit.h>

@interface CustomAnimationTransition : NSObject<UIViewControllerAnimatedTransitioning>
@property (assign,nonatomic)BOOL presented;
@end

.m文件:主要实现下面这两个方法来设置自己需要的动画过渡

#import "CustomAnimationTransition.h"
#import "UIView+Extension.h"

const CGFloat duration = 1.0f;

@implementation CustomAnimationTransition

#pragma mark -<UIViewControllerAnimatedTransitioning>
//动画时间
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return duration;
}
//设置过渡动画(modal和dismiss的动画都需要在这里处理)
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    // UITransitionContextToViewKey,
    // UITransitionContextFromViewKey.

    //出来的动画
    if (self.presented) {

        UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
        //toView.y = -toView.height;  //设置动画从上往下进来
        toView.x = toView.width;      //设置动画从右往左进来

        //设置动画3D旋转
        //toView.layer.transform = CATransform3DMakeRotation(M_PI_2, 1, 1, 0);

        [UIView animateWithDuration:duration animations:^{

            //toView.y = 0;
            toView.x = 0;

            //toView.layer.transform = CATransform3DIdentity;

        } completion:^(BOOL finished) {

            //动画完成后,视图上的事件才能处理
            [transitionContext completeTransition:YES];
        }];
    }
    //销毁的动画
    else
    {
        [UIView animateWithDuration:duration animations:^{

            UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];

            //fromView.y = -fromView.height;
            fromView.x = -fromView.width;     //从右往左出去

            //fromView.layer.transform = CATransform3DMakeRotation(M_PI_2, 1, 1, 0);

        } completion:^(BOOL finished) {

            //动画完成后,视图上的事件才能处理
            [transitionContext completeTransition:YES];
        }];
    }

}
@end

创建一个单例的动画类,将ViewController控制器类中实现的UIViewControllerTransitionDelegate协议的方法全部在该类Transition.m文件中实现,主控制器只需要创建这个单例类对象并将它设置为代理即可。

.h文件:

#import <UIKit/UIKit.h>
#import "Singleton.h"

@interface Transition : NSObject<UIViewControllerTransitioningDelegate>
SingletonH(Transition);
@end

.m文件:返回动画展示样式和动画过渡样式

#import "Transition.h"
#import "CustomPresentationController.h"
#import "CustomAnimationTransition.h"

@implementation Transition
SingletonM(Transition);

#pragma mark - <UIViewControllerTransitioningDelegate>
//返回展示样式
-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
    return [[CustomPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
}

//展示的动画
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    CustomAnimationTransition *animation = [[CustomAnimationTransition alloc]init];
    animation.presented = YES;
    return animation;
}

//关闭时的动画
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    CustomAnimationTransition *animation = [[CustomAnimationTransition alloc]init];
    animation.presented = NO;
    return animation;
}
@end

在ViewController的.m文件中实现动画测试如下:

#import "ViewController.h"
#import "SecondViewController.h"
#import "UIView+Extension.h"
#import "Transition.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    SecondViewController *second = [[SecondViewController alloc]init];

    //设置展示样式(自定义)
    second.modalPresentationStyle = UIModalPresentationCustom;//设置过渡代理(UIPresentationController)
    second.transitioningDelegate = [Transition sharedTransition];

    [self presentViewController:second animated:YES completion:nil];
}
@end

演示结果:从右往左进入,接着往左出去

时间: 2024-08-10 00:16:29

iOS:自定义模态动画 --UIPresentationController的相关文章

iOS自定义转场动画实战讲解

iOS自定义转场动画实战讲解 转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerAnimated:completion:这一组函数以模态视图的方式展现.隐藏视图.如果用到了navigationController,还可以调用pushViewController:animated:和popViewController这一组函数将新的视图控制器压栈.弹栈. 下图中所有转场动画都是自定义的

iOS 自定义页面的切换动画与交互动画

在iOS7之前,开发者为了寻求自定义Navigation Controller的Push/Pop动画,只能受限于子类化一个UINavigationController,或是用自定义的动画去覆盖它.但是随着iOS7的到来,Apple针对开发者推出了新的工具,以更灵活地方式管理UIViewController切换. 自定义导航栏的Push/Pop动画 为了在基于UINavigationController下做自定义的动画切换,先建立一个简单的工程,这个工程的rootViewController是一个

iOS 自定义页面的切换动画与交互动画 By Swift(转)

交互动画切换动画Swiftiosios7 目录(?)[-] 最终效果预览 自定义导航栏的PushPop动画 自定义Modal的PresentDismiss动画 自定义导航栏的交互式动画 使用UIPercentDrivenInteractiveTransition 自定义交互控制器 最终效果 UPDATED 在iOS7之前,开发者为了寻求自定义Navigation Controller的Push/Pop动画,只能受限于子类化一个UINavigationController,或是用自定义的动画去覆盖

iOS自定义segue和自定义segue转场动画

自定义segue需要继承 UIStoryboardSegue类,然后重写-(void)perform;方法.在方法中可以自定义转场动画. </pre><span style="color: rgb(97, 34, 174); font-family: Courier; font-size: 18px;">CoolSegue.h文件</span><p></p><p></p><pre code_sni

IOS 自定义转场动画

1,firstVCView 为你当前view 2,secondVCView 为你需要跳转到的view 3,viewCon 同第二项不要传入view直接传入对象即可.  *这里是调用   UIViewObject  为你要跳转的类  id是你需要传递的值 let view = UIViewObject()  view.id = self.id self.performAnimationBack(self.view, secondVCView: lineList.view, viewCon: lin

自定义ModalViewController 动画效果

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

Android自定义Transition动画

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

iOS 转场动画

Inherits from CAAnimation : NSObject Conforms to NSCoding (CAAnimation)NSCopying (CAAnimation)CAAction (CAAnimation)CAMediaTiming (CAAnimation)NSObject (NSObject) Framework /System/Library/Frameworks/QuartzCore.framework Availability Available in iOS

iOS开发之动画编程的几种方法

iOS开发之动画编程的几种方法 IOS中的动画总结来说有五种:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKeyframeAnimation>,NSTimer 这里我就总结了一下这五种方法,其实iOS开发中动画的编程都会在这里面变化,所以只要弄懂了这些动画编程就不难了. 一:UIView动画 一般方式 [UIView beginAnimations:@"ddd" context:nil];/