iOS 轉(zhuǎn)場(chǎng)動(dòng)畫

model類型的轉(zhuǎn)場(chǎng)動(dòng)畫系統(tǒng)默認(rèn)提供了4個(gè)

UIModalTransitionStyleCoverVertical,   //垂直上入(默認(rèn))
UIModalTransitionStyleFlipHorizontal,  //水平反轉(zhuǎn)
UIModalTransitionStyleCrossDissolve,   //漸顯效果 
UIModalTransitionStylePartialCurl      //翻頁效果
//使用方式
QMoreViewController *moreVC = [[QMoreViewController alloc] init];
 moreVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
moreVC.transitioningDelegate = animation;
[self presentViewController:moreVC animated:YES completion:nil}];
自定義的話 建議構(gòu)造一個(gè)轉(zhuǎn)場(chǎng)對(duì)象,具體如下QTranstionAnimation對(duì)象
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, QTranstionAnimationType) {
    QTranstionAnimationType_FromTop,   //上進(jìn)上出
    QTranstionAnimationType_FadeInOut   //淡入淡出
};

@interface QTranstionAnimation : NSObject<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>

//初始化
+(instancetype)shareInstance;

/**
 動(dòng)畫類型
 */
@property (nonatomic, assign) QTranstionAnimationType animatType;

@end

#import "QTranstionAnimation.h"
#import "QPresentationController.h"

static QTranstionAnimation *transtion = nil;

@interface QTranstionAnimation()
@property (nonatomic, assign) BOOL isPresnted;
@end;

@implementation QTranstionAnimation
//構(gòu)造單例
+(instancetype)shareInstance{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        transtion = [[super allocWithZone:nil] init];
    });
    return transtion;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    return [QTranstionAnimation shareInstance];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [QTranstionAnimation shareInstance];
}

#pragma mark - UIViewControllerContextTransitioning
- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
        
    if (self.isPresnted == YES) {
        //1.取出view
        UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
        //2.放入containerView
        [[transitionContext containerView] addSubview:presentedView];
        //3.自定義添加動(dòng)畫
        if(self.animatType == QTranstionAnimationType_FromTop){
            
            presentedView.frame = CGRectMake(0, -kScreenH, kScreenW, kScreenH);
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                presentedView.frame = CGRectMake(0, 0, kScreenW, kScreenH);
            }completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
            
        }else if(self.animatType == QTranstionAnimationType_FadeInOut){
            
            presentedView.alpha = 0;
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                presentedView.alpha = 1.0;
            }completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
        }
  
    } else {
        //1.取出view
        UIView *dismissedView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        //2.放入containerView
        [[transitionContext containerView]addSubview:dismissedView];
        //3.自定義添加動(dòng)畫
        if(self.animatType == QTranstionAnimationType_FromTop){
            
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                dismissedView.frame =CGRectMake(0, -kScreenH, kScreenW, kScreenH);
            }completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
            
        }else if (self.animatType == QTranstionAnimationType_FadeInOut){
            
            dismissedView.alpha = 1;
            [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
                dismissedView.alpha = 0;
            }completion:^(BOOL finished) {
                [transitionContext completeTransition:YES];
            }];
        }
    }
}

- (NSTimeInterval)transitionDuration:(nullable id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5f;
}

#pragma mark - UIViewControllerTransitioningDelegate
//設(shè)置負(fù)責(zé)進(jìn)場(chǎng)的對(duì)象
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.isPresnted = YES;
    return self;
}
//設(shè)置負(fù)責(zé)出場(chǎng)的對(duì)象
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.isPresnted = NO;
    return self;
}
//以下兩個(gè)方法負(fù)責(zé)交互處理
//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator;
//- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator;

//該方法為對(duì)容器的處理
- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0){
    
    QPresentationController *persentVC = [[QPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
    persentVC.contentFrame = CGRectMake((kScreenW-300)/2, (kScreenH-400)/2, 300, 400);
    return persentVC;
}
@end

自定義容器為QPresentationController

#import "QPresentationController.h"

@interface QPresentationController()
@property (nonatomic, strong) UIView *coverView;//蒙板
@end

@implementation QPresentationController

-(void)containerViewWillLayoutSubviews{
    [super containerViewWillLayoutSubviews];
    
    self.presentedView.frame = self.contentFrame;
    [self.containerView insertSubview:self.coverView atIndex:0];
    
}

-(void)didMissAction{
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}

-(UIView *)coverView{
    if(!_coverView){
        _coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _coverView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.3];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didMissAction)];
        [_coverView addGestureRecognizer:tap];
    }
    return _coverView;
}

@end

最后 具體使用

QTranstionAnimation *animation = [QTranstionAnimation shareInstance];
 QMoreViewController *moreVC = [[QMoreViewController alloc] init];
moreVC.modalPresentationStyle = UIModalPresentationCustom;
                    moreVC.transitioningDelegate = animation;
 [self presentViewController:moreVC animated:YES completion:^{
                        
                    }];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容