iOS自定義彈窗UIPresentationController

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

UIPresentationController彈窗.png
UIPopoverPresentationController彈窗.png

一. UIPresentationController 彈窗

1. 第一步:PresentationController 主要用于自定義設置彈框背景和要顯示的內容大小,繼承自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 是在呈現過渡即將開始的時候被調用的。我們在這個方法中把半透明黑色背景 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 方法來定義這個最終位置, 將決定PresentVC內容的位置
- (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上要顯示的內容

//  ————————————— 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 做彈窗

// 隨便一個按鈕的點擊事件
- (void)presentButtonAction {
    
    PresentVC *presentVC = [[PresentVC alloc] init];
    // 設置 動畫樣式
    presentVC.modalPresentationStyle = UIModalPresentationCustom;
    //presentVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    // 此對象要實現 UIViewControllerTransitioningDelegate 協議
    presentVC.transitioningDelegate = self;
    [self presentViewController:presentVC animated:YES completion:nil];
}

// delegate-彈出視圖代理

// 返回控制控制器彈出動畫的對象
/**
 presentedViewController     將要跳轉到的目標控制器
 presentingViewController    跳轉前的原控制器
 */
- (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的屬性。使用的的時候,需要創建的是UIViewController。UIViewController自定義內容
    UIViewController *popVC = [[UIViewController alloc] init];
    popVC.modalPresentationStyle = UIModalPresentationPopover;
    // 彈出視圖的大小
    popVC.preferredContentSize = CGSizeMake(100, 200);

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

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

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

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

// 控制器是透明的, 添加需要的控件
- (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;
}

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

都擼到這里了還不給個??

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,117評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,860評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,128評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,291評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,025評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,421評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,477評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,642評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,177評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,970評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,157評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,717評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,410評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,821評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,053評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,896評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,157評論 2 375

推薦閱讀更多精彩內容