iOS 封裝Modal動畫代碼

來源:互聯網
上載者:User

標籤:

1.自訂轉場動畫要寫的代碼很多,如果整個項目的轉場動畫都必須一致,則必須考慮把modal代碼封裝起來

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

    second.modalPresentationStyle = UIModalPresentationCustom;

    second.transitioningDelegate = 自訂一個代理;

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

 

2. 自訂代理對象

// MYTransition.h

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

#import "Singleton.h"

@interface MYTransition : NSObject<UIViewControllerTransitioningDelegate>

SingletonH(MYTransition)

@end

 

// MYTransition.m

#import "MYTransition.h"

#import "MYPresentationController.h"

#import "MYAnimatedTransition.h"

#import "UIView+MJ.h"

@implementation MYTransition

SingletonM(MYTransition)

#pragma mark - UIViewControllerTransitioningDelegate

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

{

    return [[MYPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];

}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source

{

    MYAnimatedTransition *anima = [[MYAnimatedTransition alloc]init];

    anima.show = YES;

    return anima;

}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed

{

    MYAnimatedTransition *anima = [[MYAnimatedTransition alloc]init];

    anima.show = NO;

    return anima;

}

@end

 

3. 使用封裝好的Modal動畫,就非常簡單了

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

    second.modalPresentationStyle = UIModalPresentationCustom;

    second.transitioningDelegate = [MYTransition sharedMYTransition];    

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

 

iOS 封裝Modal動畫代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.