UIVisualEffectView in iOS 10

后端 未结 3 940
一向
一向 2020-12-24 12:12

I am presenting a UIViewController that contains a UIVisualEffectView as follows:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtInd         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 12:32

    Code below blur parent view controller when ViewController presented. Tested on both iOS9 and 10.

    @interface ViewController () 
    
    @property (nonatomic) UIVisualEffectView *blurView;
    
    @end
    
    
    @implementation ViewController
    
    - (instancetype)init
    {
        self = [super init];
        if (!self)
            return nil;
    
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        self.transitioningDelegate = self;
    
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.view.backgroundColor = [UIColor clearColor];
        self.blurView = [UIVisualEffectView new];
        [self.view addSubview:self.blurView];
        self.blurView.frame = self.view.bounds;
    }
    
    - (id )animationControllerForPresentedController:(UIViewController *)presented
                                                                           presentingController:(UIViewController *)presenting
                                                                       sourceController:(UIViewController *)source
    {
        return self;
    }
    
    -(NSTimeInterval)transitionDuration:(id)transitionContext
    {
        return 0.3;
    }
    
    -(void)animateTransition:(id)transitionContext
    {
        UIView *container = [transitionContext containerView];
    
        [container addSubview:self.view];
    
        self.blurView.effect = nil;
    
        [UIView animateWithDuration:0.3 animations:^{
            self.blurView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:finished];
        }];
    }
    
    @end
    

提交回复
热议问题