iOS UIPresentationController实现弹出视图

来源:互联网 发布:linux虚拟机网络不通 编辑:程序博客网 时间:2024/05/17 09:36
 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;



}

0 0
原创粉丝点击