UIPresentationController 蒙版阴影,看我就足够了(特简单)

35 篇文章 0 订阅
28 篇文章 0 订阅

因为项目需要,需要在本页面弹出一个带阴影的页面,方法有很多。你可以自定义一个view或者viewController,今天简单介绍一下,使用viewController的方法
离不开UIPresentationController,8.0有的API,我觉得方便不少
把你想要的效果封装一个对象中(特简单)

- UIPresentationController的作用

1.管理所有Modal出来的控制器
2.管理通过这个方法
-(void) presentViewController:(UIViewController *) animated:(BOOL) completion:^(void)completion;显示出来的控制器
3.管理\监听 切换控制器的过程
4控制器一旦调了present方法,控制器的presentationController,会先创建好了,然后整个控制器的切换由presentationController管理
5.控制器有2个属性:presentationController和poppverPresentationController
6.设置控制器以popver的方式切换.
也就是说 控制器的popoverPresentationController属性就不再是nil了 而且popoverPresentationController 和 presentationController 指向同一对象,指向的都是 UIPopoverPresentationController 类对象
注意:UIPopoverPresentationController类 继承自 UIPresentationController类

上代码

XWShadow.h

#import <UIKit/UIKit.h>

@interface XWShadow : UIPresentationController
@property (nonatomic, strong) UIVisualEffectView *visualView;
@end

XWShadow.m

//presentationTransitionWillBegin 是在呈现过渡即将开始的时候被调用的。我们在这个方法中把半透明黑色背景 View 加入到 containerView 中,并且做一个 alpha 从0到1的渐变过渡动画。
- (void) presentationTransitionWillBegin
{

    //使用UIVisualEffectView实现模糊效果
    UIBlurEffect *blur  = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    _visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
    _visualView.frame = self.containerView.bounds;
    _visualView.alpha = 0.4;
    _visualView.backgroundColor = [UIColor blackColor];

    [self.containerView addSubview:_visualView];

}
//presentationTransitionDidEnd: 是在呈现过渡结束时被调用的,并且该方法提供一个布尔变量来判断过渡效果是否完成。在我们的例子中,我们可以使用它在过渡效果已结束但没有完成时移除半透明的黑色背景 View。
-(void)presentationTransitionDidEnd:(BOOL)completed {

    // 如果呈现没有完成,那就移除背景 View
    if (!completed) {

        [_visualView removeFromSuperview];

    }

}
//以上就涵盖了我们的背景 View 的呈现部分,我们现在需要给它添加淡出动画并且在它消失后移除它。正如你预料的那样,dismissalTransitionWillBegin 正是我们把它的 alpha 重新设回0的地方。
-(void)dismissalTransitionWillBegin {

    _visualView.alpha = 0.0;

}
//我们还需要在消失完成后移除背景 View。做法与上面 presentationTransitionDidEnd: 类似,我们重载 dismissalTransitionDidEnd: 方法
-(void)dismissalTransitionDidEnd:(BOOL)completed{


    if (completed) {

        [_visualView removeFromSuperview];
    }
}


//还有最后一个方法需要重载。在我们的自定义呈现中,被呈现的 view 并没有完全完全填充整个屏幕,而是很小的一个矩形。被呈现的 view 的过渡动画之后的最终位置,是由 UIPresentationViewController 来负责定义的。我们重载 frameOfPresentedViewInContainerView 方法来定义这个最终位置
- (CGRect)frameOfPresentedViewInContainerView
{

    CGFloat windowH = [UIScreen mainScreen].bounds.size.height;

    CGFloat windowW = [UIScreen mainScreen].bounds.size.width;

    self.presentedView.frame = CGRectMake(0, windowH - 298, windowW, 298);


    return self.presentedView.frame;
}

把你想要展示的页面WantController 显示出来

WantController *xw = [[WantController alloc]init];
xw.modalPresentationStyle = UIModalPresentationCustom;// 设置 动画样式
xw.transitioningDelegate = self;// 此对象要实现 UIViewControllerTransitioningDelegate 协议
[self presentViewController:xw animated:YES completion:^{
    }];

记住要写UIViewControllerTransitioningDelegate

// 返回控制控制器弹出动画的对象
/**
 *  参数: presentedViewController     将要跳转到的目标控制器
 presentingViewController    跳转前的原控制器
 */
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值