首页 > 其他分享 >iOS开发_具有毛玻璃效果的modal控制器

iOS开发_具有毛玻璃效果的modal控制器

时间:2023-01-06 11:14:05浏览次数:44  
标签:blurStyle self iOS Controller GC modal Blur UIViewController 毛玻璃

1、使用

  • 新建控制器时继承 GC_Blur_Controller,然后按照正常的modal控制器使用即可。
  • 1.1 继承

#import "GC_Blur_Controller.h"

@interface Presented_Controller : GC_Blur_Controller

@end
  • 1.2 使用

Presented_Controller *pvc = [[Presented_Controller alloc] init];
pvc.blurStyle = UIBlurEffectStyleLight;
[self presentViewController:pvc animated:YES completion:nil];

2、实现代码

  • 2.1 GC_Blur_Controller.h

#import <UIKit/UIKit.h>

@interface GC_Blur_Controller : UIViewController

@property(nonatomic, assign) UIBlurEffectStyle blurStyle;

@end
  • 2.2 GC_Blur_Controller.m

#import "GC_Blur_Controller.h"

@interface GC_Blur_Presenter : NSObject <UIViewControllerTransitioningDelegate>

@property(nonatomic, assign) UIBlurEffectStyle blurStyle;

@end

@class GC_Blur_Presentation_Controller;
@protocol GC_Blur_Presentation_Controller_Delegate <NSObject>

- (void)presentationControllerDidDismissed:(GC_Blur_Presentation_Controller *)controller;

@end

@interface GC_Blur_Presentation_Controller : UIPresentationController

@property(nonatomic, assign) UIBlurEffectStyle blurStyle;

@property(nonatomic, weak) id<GC_Blur_Presentation_Controller_Delegate> gc_delegate;

- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController
                       presentingViewController:(UIViewController *)presentingViewController
                                          style:(UIBlurEffectStyle)style;

@end

@interface GC_Blur_Transitioning : NSObject <UIViewControllerAnimatedTransitioning>

@property(nonatomic, assign) BOOL isPresentation;

@end

@interface GC_Blur_Controller ()

@property(nonatomic, strong) GC_Blur_Presenter *presenter;

@end

@implementation GC_Blur_Controller

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self vv_commonSetup];
    }
    return self;
}

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

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor clearColor];
}

- (void)vv_commonSetup {
    self.modalPresentationStyle = UIModalPresentationCustom;
    
    _presenter = [[GC_Blur_Presenter alloc] init];
    _blurStyle = UIBlurEffectStyleDark;
    _presenter.blurStyle = _blurStyle;
    
    self.transitioningDelegate = _presenter;
}

- (void)setBlurStyle:(UIBlurEffectStyle)blurStyle {
    if (blurStyle != _blurStyle) {
        _blurStyle = blurStyle;
        _presenter.blurStyle = blurStyle;
    }
}

@end

@interface GC_Blur_Presenter () <GC_Blur_Presentation_Controller_Delegate>

@property(nonatomic, strong) GC_Blur_Presentation_Controller *animationController;

@end

@implementation GC_Blur_Presenter

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

- (GC_Blur_Presentation_Controller *)presentationControllerForPresentedViewController:(UIViewController *)presented
                                                           presentingViewController:(UIViewController *)presenting
                                                               sourceViewController:(UIViewController *)source {
    if (!self.animationController) {
        self.animationController = [[GC_Blur_Presentation_Controller alloc] initWithPresentedViewController:presented presentingViewController:presenting style:self.blurStyle];
        self.animationController.gc_delegate = self;
    }
    return self.animationController;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                 presentingController:(UIViewController *)presenting
                                                                     sourceController:(UIViewController *)source {
    
    GC_Blur_Transitioning *transition = [[GC_Blur_Transitioning alloc] init];
    transition.isPresentation = YES;
    return transition;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    
    GC_Blur_Transitioning *transition = [[GC_Blur_Transitioning alloc] init];
    transition.isPresentation = NO;
    return transition;
}

- (void)setBlurStyle:(UIBlurEffectStyle)blurStyle {
    if (_blurStyle != blurStyle) {
        _blurStyle = blurStyle;
        self.animationController.blurStyle = blurStyle;
    }
}

- (void)presentationControllerDidDismissed:(GC_Blur_Presentation_Controller *)controller {
    self.animationController = nil;
}

@end

@interface GC_Blur_Presentation_Controller ()

@property(nonatomic, strong) UIView *effectContainerView;

@property(nonatomic, strong) UIVisualEffectView *dimmingView;

@end

@implementation GC_Blur_Presentation_Controller

- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController style:(UIBlurEffectStyle)style {
    self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
    if (self) {
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:style];
        _dimmingView = [[UIVisualEffectView alloc] initWithEffect:effect];
        _blurStyle = style;
        _effectContainerView = [[UIView alloc] init];
        _effectContainerView.alpha = 0.0;
    }
    return self;
}

- (void)presentationTransitionWillBegin {
    self.effectContainerView.frame = self.containerView.bounds;
    self.dimmingView.frame = self.containerView.bounds;

    [self.effectContainerView insertSubview:self.dimmingView atIndex:0];
    [self.containerView insertSubview:self.effectContainerView atIndex:0];
    
    self.effectContainerView.alpha = 0.0;
    
    id <UIViewControllerTransitionCoordinator> coordinator = [self.presentedViewController transitionCoordinator];
    if (coordinator) {
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            self.effectContainerView.alpha = 1.0;
        } completion:nil];
    }
    else {
        self.effectContainerView.alpha = 1.0;
    }
}

- (void)dismissalTransitionWillBegin {
    id <UIViewControllerTransitionCoordinator> coordinator = [self.presentedViewController transitionCoordinator];
    if (coordinator) {
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
            self.effectContainerView.alpha = 0.0;
        } completion:nil];
    }
    else {
        self.effectContainerView.alpha = 0.0;
    }
}

- (void)dismissalTransitionDidEnd:(BOOL)completed {
    if (completed) {
        if (self.gc_delegate && [self.gc_delegate respondsToSelector:@selector(presentationControllerDidDismissed:)]) {
            [self.gc_delegate presentationControllerDidDismissed:self];
        }
    }
}

- (void)containerViewWillLayoutSubviews {
    self.effectContainerView.frame = self.containerView.bounds;
    self.dimmingView.frame = self.containerView.bounds;
    self.presentedView.frame = self.containerView.bounds;
}

- (BOOL)shouldPresentInFullscreen {
    return YES;
}

- (UIModalPresentationStyle)adaptivePresentationStyle {
    return UIModalPresentationCustom;
}

- (void)setBlurStyle:(UIBlurEffectStyle)blurStyle {
    if (blurStyle != _blurStyle) {
        _blurStyle = blurStyle;
        
        UIView *previousDimmingView = _dimmingView;
        
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:blurStyle];
        self.dimmingView = [[UIVisualEffectView alloc] initWithEffect:effect];
        NSArray *subviews = [self.effectContainerView subviews];
        for (UIView *view in subviews) {
            if (view == previousDimmingView) {
                self.dimmingView.frame = previousDimmingView.frame;
                [self.effectContainerView insertSubview:self.dimmingView aboveSubview:previousDimmingView];
                [previousDimmingView removeFromSuperview];
            }
        }
    }
}

@end

@implementation GC_Blur_Transitioning

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

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    UIView *containerView = [transitionContext containerView];
    if (self.isPresentation) {
        [containerView addSubview:toView];
    }
    
    UIViewController *animatingViewController = self.isPresentation ? toViewController : fromViewController;
    UIView *animatingView = self.isPresentation ? toView : fromView;
    
    CGRect onScreenFrame = [transitionContext finalFrameForViewController:animatingViewController];
    CGRect offScreenFrame = CGRectOffset(onScreenFrame, 0, onScreenFrame.size.height);
    
    CGRect initialFrame = self.isPresentation ? offScreenFrame : onScreenFrame;
    CGRect finalFrame = self.isPresentation ? onScreenFrame : offScreenFrame;
    animatingView.frame = initialFrame;
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 usingSpringWithDamping:300.0 initialSpringVelocity:5.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState animations:^{
        animatingView.frame = finalFrame;
    } completion:^(BOOL finished) {
        if (!self.isPresentation) {
            [fromView removeFromSuperview];
        }
        [transitionContext completeTransition:YES];
    }];
}

@end

标签:blurStyle,self,iOS,Controller,GC,modal,Blur,UIViewController,毛玻璃
From: https://www.cnblogs.com/CH520/p/17029833.html

相关文章

  • 与Xcode相比Adobe AIR开发iOS的优势和局限
    AIR的优势AIR的优势其实就是Flash或者ActionScript语言的优势。这些优势大家已经在互联网上看过许多了,我还是啰嗦一下:1.优秀的2D性能和渲染机制网络上关于Flash性能底下的......
  • iOS上架流程​
    ​有3D-touch机型的坑​昨天在上线的时候遇到了一个坑,最后导致的结果是找了好几个小时,直接到半夜才能上线。​入正题:坑是:项目运行在456上没什么问题,但是在6S以上的机型就有......
  • Ios上架时间​
    ​苹果APP上架难,不用越狱,下载未上架APP!​苹果的ios系统生态一项是流畅安全的,没有乱七八糟的东西,APP都是在苹果AppStore应用商店来下载,对于开发者来说,一个APP上架需要经历严......
  • Ios上架需要多久​
    ​很多同学都觉得公有云、私有云、混合云这些概念的认识比较混乱,开始我面对网上很多专业的解释不明所以,直到我遇到这个说法:​大白话解释一下​你娶了一个老婆,这叫传统IT架构......
  • Ios上架app流程​
    ​一、上架基本需求资料​1、苹果开发者账号(公司已有可以不用申请,需要开通开发者功能,每年99美元)​2、开发好的APP​二、证书​上架版本需要使用正式的证书​1、创建证书A......
  • Ios app如何上架
    ​前几天在忙着上线,尽管之前已经上线过一次,但由于本身比较菜,还是状况百出.好在今天终于成功提交,因此来写写心得.​接下来我讲一讲关于我版本更新过程中的一些问题.​Bu......
  • iOSApp上架流程
    ​注意:下载AU后需安装java8运行环境才能启动;安装好了最新Java8运行环境,解压AU软件包,无需安装,Windows64位系统点击appuploader.exe启动;如果是Linux/Windows32位系统......
  • Iosapp上架流程
    ​在使用H5混合开发的app打包后,需要将ipa文件上传到appstore进行发布,就需要去苹果开发者中心进行发布。​但是在苹果开发者中心无法直接上传ipa文件,它要求我们使用xcode或tr......
  • linux C 简易版iostat
    Linux下用C语言获取IO信息只获取iostat的CPU信息代码如下#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>#include<unistd.h>#inc......
  • iOSApp上架流程
     ​注意:下载AU后需安装java8运行环境才能启动;安装好了最新Java8运行环境,解压AU软件包,无需安装,Windows64位系统点击appuploader.exe启动;如果是Linux/Windows......