iOS:模態(tài)之蒙版設(shè)置

MZBasePresentFadeAnimationVC.h
NS_ASSUME_NONNULL_BEGIN

@interface MZBasePresentFadeAnimationVC : UIViewController

///蒙版
@property (nonatomic, strong) UIView *backgroundView;
///點(diǎn)擊蒙版是否dismiss掉當(dāng)前控制器, 默認(rèn)為YES
@property (nonatomic, assign) BOOL outsideTapDismissEnable;
///dismiss結(jié)束后的回調(diào)
@property (nonatomic, copy) void (^dismissComplete)(void);

@end

NS_ASSUME_NONNULL_END
MZBasePresentFadeAnimationVC.m
#import "MZBasePresentFadeAnimationVC.h"

//MARK: - MZBaseAnimation
@interface MZBaseAnimation : NSObject<UIViewControllerAnimatedTransitioning>

/// present or dismiss
@property (nonatomic, assign, readonly) BOOL isPresenting;

+ (instancetype)alertAnimationIsPresenting:(BOOL)isPresenting;

@end

//MARK: - MZBasePresentFadeAnimationVC
@interface MZBasePresentFadeAnimationVC ()<UIViewControllerTransitioningDelegate>

@property (nonatomic, weak) UITapGestureRecognizer *singleTap;

@end

@implementation MZBasePresentFadeAnimationVC

- (instancetype)init {
    if (self = [super init]) {
        [self configController];
    }
    return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self configController];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addBackgroundView];
    [self addSingleTapGesture];
}

- (void)configController {
    self.providesPresentationContextTransitionStyle = YES;
    self.definesPresentationContext = YES;
    self.modalPresentationStyle = UIModalPresentationCustom;
    self.outsideTapDismissEnable = YES;
    self.transitioningDelegate = self;
}

- (void)addBackgroundView {
    if (self.backgroundView == nil) {
        UIView *backgroundView = [[UIView alloc] initWithFrame:UIScreen.mainScreen.bounds];
        backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        self.backgroundView = backgroundView;
    }
    self.backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view insertSubview:self.backgroundView atIndex:0];
}

- (void)addSingleTapGesture {
    self.view.userInteractionEnabled = YES;
    self.backgroundView.userInteractionEnabled = YES;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
    singleTap.enabled = self.outsideTapDismissEnable;

    [self.backgroundView addGestureRecognizer:singleTap];
    self.singleTap = singleTap;
}

- (void)onTap:(UITapGestureRecognizer *)tapGesture {
    [self dismissViewControllerAnimated:YES];
}

- (void)dismissViewControllerAnimated:(BOOL)animated {
    [self dismissViewControllerAnimated:YES completion:self.dismissComplete];
}

@end


@implementation MZBasePresentFadeAnimationVC (TransitionAnimate)

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [MZBaseAnimation alertAnimationIsPresenting:YES];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [MZBaseAnimation alertAnimationIsPresenting:NO];
}

@end



//MARK: - MZBaseAnimation
@interface MZBaseAnimation ()
@property (nonatomic, assign) BOOL isPresenting;
@end

@implementation MZBaseAnimation

- (instancetype)initWithIsPresenting:(BOOL)isPresenting {
    if (self = [super init]) {
        self.isPresenting = isPresenting;
    }
    return self;
}

+ (instancetype)alertAnimationIsPresenting:(BOOL)isPresenting {
    return [[self alloc]initWithIsPresenting:isPresenting];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.isPresenting) {
        return 0.45;
    }
    return 0.25;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (_isPresenting) {
        [self presentAnimateTransition:transitionContext];
    } else {
        [self dismissAnimateTransition:transitionContext];
    }
}

- (void)presentAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    MZBasePresentFadeAnimationVC *alertController = (MZBasePresentFadeAnimationVC *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    alertController.backgroundView.alpha = 0.0;
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:alertController.view];

    [UIView animateWithDuration:0.25 animations:^{
        alertController.backgroundView.alpha = 1.0;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

- (void)dismissAnimateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    MZBasePresentFadeAnimationVC *alertController = (MZBasePresentFadeAnimationVC *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    [UIView animateWithDuration:0.25 animations:^{
        alertController.backgroundView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

@end

使用

只需要將需要模態(tài)出來的VC繼承與MZBasePresentFadeAnimationVC即可。

- (IBAction)show:(id)sender {
    DetailViewController *vc = [[DetailViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市篓像,隨后出現(xiàn)的幾起案子霉咨,更是在濱河造成了極大的恐慌,老刑警劉巖讼溺,帶你破解...
    沈念sama閱讀 211,884評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡授滓,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,347評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門肆糕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來般堆,“玉大人,你說我怎么就攤上這事诚啃』此ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 157,435評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵始赎,是天一觀的道長和橙。 經(jīng)常有香客問我,道長造垛,這世上最難降的妖魔是什么魔招? 我笑而不...
    開封第一講書人閱讀 56,509評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮五辽,結(jié)果婚禮上办斑,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好乡翅,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,611評(píng)論 6 386
  • 文/花漫 我一把揭開白布鳞疲。 她就那樣靜靜地躺著,像睡著了一般蠕蚜。 火紅的嫁衣襯著肌膚如雪尚洽。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,837評(píng)論 1 290
  • 那天波势,我揣著相機(jī)與錄音翎朱,去河邊找鬼。 笑死尺铣,一個(gè)胖子當(dāng)著我的面吹牛拴曲,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播凛忿,決...
    沈念sama閱讀 38,987評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼澈灼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了店溢?” 一聲冷哼從身側(cè)響起叁熔,我...
    開封第一講書人閱讀 37,730評(píng)論 0 267
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎床牧,沒想到半個(gè)月后荣回,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,194評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡戈咳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,525評(píng)論 2 327
  • 正文 我和宋清朗相戀三年心软,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片著蛙。...
    茶點(diǎn)故事閱讀 38,664評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡删铃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出踏堡,到底是詐尸還是另有隱情猎唁,我是刑警寧澤,帶...
    沈念sama閱讀 34,334評(píng)論 4 330
  • 正文 年R本政府宣布顷蟆,位于F島的核電站诫隅,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏帐偎。R本人自食惡果不足惜逐纬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,944評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望肮街。 院中可真熱鬧风题,春花似錦判导、人聲如沸嫉父。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,764評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绕辖。三九已至摇肌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間仪际,已是汗流浹背围小。 一陣腳步聲響...
    開封第一講書人閱讀 31,997評(píng)論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留树碱,地道東北人肯适。 一個(gè)月前我還...
    沈念sama閱讀 46,389評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像成榜,于是被迫代替她去往敵國和親框舔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,554評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容