本文主要介紹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;
}
都擼到這里了還不給個??