iOS自定義彈窗UIPresentationController

本文主要介紹UIPresentationController和UIPopoverPresentationController的使用.
UIPresentationController 是 iOS8 新增的一個(gè) API,可以用它實(shí)現(xiàn)自定義的彈窗.
UIPopoverPresentationController是UIPresentationController的子類, 添加了指向箭頭, 但比較丑, 一般需要自定義. 擼到底部有驚喜哦??

UIPresentationController彈窗.png
UIPopoverPresentationController彈窗.png

一. UIPresentationController 彈窗

1. 第一步:PresentationController 主要用于自定義設(shè)置彈框背景和要顯示的內(nèi)容大小,繼承自UIPresentationController

//  ————————————— PresentationController.h
#import <UIKit/UIKit.h>

@interface PresentationController : UIPresentationController

@end


//  ————————————— PresentationController.m

#import "PresentationController.h"

@interface PresentationController ()

@property (nonatomic,strong) UIVisualEffectView *visualView;

@end

@implementation PresentationController

//presentationTransitionWillBegin 是在呈現(xiàn)過(guò)渡即將開(kāi)始的時(shí)候被調(diào)用的。我們?cè)谶@個(gè)方法中把半透明黑色背景 View 加入到 containerView 中,并且做一個(gè) alpha 從0到1的漸變過(guò)渡動(dòng)畫。
- (void)presentationTransitionWillBegin {
    
    // 使用UIVisualEffectView實(shí)現(xiàn)模糊效果
    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: 是在呈現(xiàn)過(guò)渡結(jié)束時(shí)被調(diào)用的,并且該方法提供一個(gè)布爾變量來(lái)判斷過(guò)渡效果是否完成。在我們的例子中,我們可以使用它在過(guò)渡效果已結(jié)束但沒(méi)有完成時(shí)移除半透明的黑色背景 View。
- (void)presentationTransitionDidEnd:(BOOL)completed {
    
    // 如果呈現(xiàn)沒(méi)有完成,那就移除背景 View
    if (!completed) {
        [_visualView removeFromSuperview];
    }
}

//以上就涵蓋了我們的背景 View 的呈現(xiàn)部分,我們現(xiàn)在需要給它添加淡出動(dòng)畫并且在它消失后移除它。正如你預(yù)料的那樣,dismissalTransitionWillBegin 正是我們把它的 alpha 重新設(shè)回0的地方。
- (void)dismissalTransitionWillBegin {
    _visualView.alpha = 0.0;
}

//我們還需要在消失完成后移除背景 View。做法與上面 presentationTransitionDidEnd: 類似,我們重載 dismissalTransitionDidEnd: 方法
- (void)dismissalTransitionDidEnd:(BOOL)completed {
    if (completed) {
        [_visualView removeFromSuperview];
    }
}

//還有最后一個(gè)方法需要重載。在我們的自定義呈現(xiàn)中,被呈現(xiàn)的 view 并沒(méi)有完全完全填充整個(gè)屏幕,而是很小的一個(gè)矩形。被呈現(xiàn)的 view 的過(guò)渡動(dòng)畫之后的最終位置,是由 UIPresentationViewController 來(lái)負(fù)責(zé)定義的。我們重載 frameOfPresentedViewInContainerView 方法來(lái)定義這個(gè)最終位置, 將決定PresentVC內(nèi)容的位置
- (CGRect)frameOfPresentedViewInContainerView {
    
    CGFloat windowH = [UIScreen mainScreen].bounds.size.height;
    CGFloat windowW = [UIScreen mainScreen].bounds.size.width;
    
    self.presentedView.frame = CGRectMake(0, windowH - 300, windowW, 300);
    
    return self.presentedView.frame;
}

@end

2. 第二步:PresentVC 主要用于自定義在PresentationController上要顯示的內(nèi)容

//  ————————————— PresentVC.h
#import <UIKit/UIKit.h>

@interface PresentVC : UIViewController

@end


//  ————————————— PresentVC.m

@implementation PresentVC

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

- (void)customView {
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.backgroundColor = [UIColor greenColor];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    cancelButton.frame = CGRectMake(0, 0, ScreenWidth, 30);
    [self.view addSubview:cancelButton];
    
    UILabel *contentLable = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(cancelButton.frame), ScreenWidth, 300-CGRectGetMaxY(cancelButton.frame))];
    contentLable.backgroundColor = [UIColor redColor];
    contentLable.text = @"contentLabel";
    contentLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:contentLable];
}

- (void)cancelButtonAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

3. 第三步:使用自定義的PresentationController和PresentVC 做彈窗

// 隨便一個(gè)按鈕的點(diǎn)擊事件
- (void)presentButtonAction {
    
    PresentVC *presentVC = [[PresentVC alloc] init];
    // 設(shè)置 動(dòng)畫樣式
    presentVC.modalPresentationStyle = UIModalPresentationCustom;
    //presentVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    // 此對(duì)象要實(shí)現(xiàn) UIViewControllerTransitioningDelegate 協(xié)議
    presentVC.transitioningDelegate = self;
    [self presentViewController:presentVC animated:YES completion:nil];
}

// delegate-彈出視圖代理

// 返回控制控制器彈出動(dòng)畫的對(duì)象
/**
 presentedViewController     將要跳轉(zhuǎn)到的目標(biāo)控制器
 presentingViewController    跳轉(zhuǎn)前的原控制器
 */
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source{
    
    return [[PresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

二. UIPopoverPresentationController 彈窗


- (IBAction)popoverButton:(UIButton *)sender {

    // UIPopoverPresentationController是iOS8以后新增的, 是UIPresentationController的子類,是UIViewController的屬性。使用的的時(shí)候,需要?jiǎng)?chuàng)建的是UIViewController。UIViewController自定義內(nèi)容
    UIViewController *popVC = [[UIViewController alloc] init];
    popVC.modalPresentationStyle = UIModalPresentationPopover;
    // 彈出視圖的大小
    popVC.preferredContentSize = CGSizeMake(100, 200);

    // 彈出視圖設(shè)置
    UIPopoverPresentationController *popver = popVC.popoverPresentationController;
    popver.delegate = self;
    //彈出時(shí)參照視圖的大小,與彈框的位置有關(guān)
    popver.sourceRect = self.popoverButton.bounds;
    //彈出時(shí)所參照的視圖,與彈框的位置有關(guān)
    popver.sourceView = self.popoverButton;
    //彈框的箭頭方向
    popver.permittedArrowDirections = UIPopoverArrowDirectionUp;

    popver.backgroundColor = [UIColor orangeColor];
    [self presentViewController:popVC animated:YES completion:nil];
}

// -------UIPopoverPresentationControllerDelegate
// 默認(rèn)返回的是覆蓋整個(gè)屏幕,需設(shè)置成UIModalPresentationNone。
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationNone;
}
// 設(shè)置點(diǎn)擊蒙版是否消失,默認(rèn)為YES
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
    return YES;
}
// 彈出視圖消失后調(diào)用的方法
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
}

偷偷告訴你:如果實(shí)在想用UIViewController控制器做彈窗,最簡(jiǎn)單的方式可以按下面方法寫,試一下有驚喜...

// 控制器是透明的, 添加需要的控件
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:blur];
    visualView.frame = self.view.bounds;
    visualView.alpha = 0.4;
    visualView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:visualView];
}

// 控制器彈出方式
- (UIModalTransitionStyle)modalTransitionStyle {
    return UIModalTransitionStyleCrossDissolve;
}

// 設(shè)置控制器透明
- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationOverCurrentContext;
}

都擼到這里了還不給個(gè)??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容