iOS UIPresentationController实现弹出视图

这篇博客介绍了如何在iOS中利用UIPresentationController实现弹出视图的效果。通过设置modalPresentationStyle为UIModalPresentationCustom,并实现UIViewControllerTransitioningDelegate协议,配合presentationControllerForPresentedViewController方法和frameOfPresentedViewInContainerView方法,可以自定义弹出视图的位置和大小,实现从底部弹出的交互体验。
 IOS 8 新加入一个类:UIPresentationController,它与 iOS 7 新添加的几个类与协议一道,帮助我们方便快捷地实现 ViewController 的自定义过渡效果。

实现自定义过渡
我们需要两个对象来实现自定义过渡,一个 UIPresentationController 的子类以及一个遵从 UIViewControllerAnimatedTransitioning 协议的类。
我们的 UIPresentationController 的子类是负责「被呈现」及「负责呈现」的 controller 以外的 controller 的,看着很绕口,说白了,在我们的例子中,它负责的仅仅是那个带渐变效果的黑色半透明背景 View。
而 UIViewControllerAnimatedTransitioning 类将会负责「被呈现」的 ViewController 的过渡动画。首先我们看 UIPresentationController。
UIPresentationController
在我们的 UIPresentationController 中,我们需要重写其中5个方法:
* presentationTransitionWillBegin
* presentationTransitionDidEnd:
* dismissalTransitionWillBegin
* dismissalTransitionDidEnd:
* frameOfPresentedViewInContainerView
presentationTransitionWillBegin 是在呈现过渡即将开始的时候被调用的。我们在这个方法中把半透明黑色背景 View 加入到 containerView 中,并且做一个 alpha 从0到1的渐变过渡动画。
核心部分代码:

实现点击按钮弹出一个对话视图,例如下单列表:

- (IBAction)ShowMenusView:(id)sender {

    UIStoryboard *board=[UIStoryboard storyboardWithName:@"Trade" bundle:nil];

    ThreeViewController *Three=[board instantiateViewControllerWithIdentifier:@"Three"];

   

    Three.modalPresentationStyle=UIModalPresentationCustom;

    Three.view.backgroundColor=[UIColor greenColor];

    Three.transitioningDelegate = self;// 此对象要实现 UIViewControllerTransitioningDelegate 协议


    [self presentViewController:Three animated:YES completion:nil];




}

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{

    

    

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


}

在新建的PresentViewController实现

- (CGRect)frameOfPresentedViewInContainerView

{

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

    

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

    

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

    //弹出视图的位置及大小设定

    self.presentedView.center=CGPointMake(windowW/2, windowH/2);

    

    return self.presentedView.frame;



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值