从iOS8开始,controller之间的跳转特效,需要用新的API UIPresentationController来实现。比如希望实现这样一个特效:显示一个模态窗口,大小和位置是自定义的,遮罩在原来的页面上。在iOS8之前,可以在viewWillAppear里设置superview的frame:
- (void)presentModal:(NSDictionary*)result
{
YLSCheckoutSignatureController *controller = [[YLSCheckoutSignatureController alloc] initWithModel:result];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
controller.modalPresentationStyle = UIModalPresentationCustom;
}else{
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
controller.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentViewController:controller animated:YES completion:nil];
}
-(void) viewWillAppear:(BOOL)animated
{
// in iOS8, handle by UIPresentationController
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
return;
}
self.view.superview.layer.cornerRadius = 10;
self.view.superview.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.view.superview.clipsToBounds = YES;
self.view.superview.frame = CGRectMake(62, 114, 900, 540);
}
但是以上的代码,在iOS8里就不再生效了,要用UIPresentationController来实现
首先明确一点,从Controller A->B,B的样式和跳转特效,还是由B来控制的。只不过以前是直接在Controller的生命周期方法里操作,而现在有专门的API来完成而已。这种设计也是合理的,否则如果从A可以跳转到B和C,但是样式和特效不一样,就只能通过在A里面设置实例变量来区分了,容易出错也很别扭。所以把跳转的行为由目标Controller来控制是很合理的
不过这组API的文档不太全,后续SDK升级可能会逐渐完善。以下介绍实现步骤:
目标Controller实现特定protocol
首先目标Controller要实现特定的协议,创建一个UIPresentationController
@interface YLSCheckoutSignatureController : UIViewController<UIScrollViewDelegate, UIViewControllerTransitioningDelegate>
self.transitioningDelegate = self;
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
return [[YLSMainPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}
当条件满足时,iOS系统会调用这个方法,于是可以实例化自定义的UIPresentationController子类,定义跳转的样式和特效
自定义UIPresentationController
然后就要实现自定义的UIPresentationController,下面这段实例代码,实现居中展示一个自定义frame的模态页面,同时有半透明背景遮住原来的页面
@implementation YLSMainPresentationController
{
UIView *dimmingView;
}
-(id) initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
{
self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];
if(self){
dimmingView = [[UIView alloc] init];
dimmingView.backgroundColor = [UIColor grayColor];
dimmingView.alpha = 0.0;
}
return self;
}
- (void)presentationTransitionWillBegin
{
dimmingView.frame = self.containerView.bounds;
[self.containerView addSubview:dimmingView];
[self.containerView addSubview:self.presentedView];
id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
dimmingView.alpha = 0.5;
} completion:nil];
}
- (void)presentationTransitionDidEnd:(BOOL)completed
{
if(!completed){
[dimmingView removeFromSuperview];
}
}
- (void)dismissalTransitionWillBegin
{
id<UIViewControllerTransitionCoordinator> coordinator = self.presentingViewController.transitionCoordinator;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
dimmingView.alpha = 0.0;
} completion:nil];
}
- (void)dismissalTransitionDidEnd:(BOOL)completed
{
if(completed){
[dimmingView removeFromSuperview];
}
}
- (CGRect)frameOfPresentedViewInContainerView
{
return CGRectMake(62.f, 114.f, 900.f, 540.f);
}
@end
代码确实比以前复杂了一点,但是其实每个生命周期方法都是比较明确的,开发者可控的粒度也更细了。比如设置presented frame,就有专门的方法,只要返回CGRect就可以了,还是比较方便的
原始的ViewController发起跳转动作
经过前面2步,当自定义跳转发生时,就可以很细致地控制样式和跳转行为。接下来就是由原始controller(presenting view controller)来发起跳转动作:
- (void)presentModal:(NSDictionary*)result
{
YLSCheckoutSignatureController *controller = [[YLSCheckoutSignatureController alloc] initWithModel:result];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
controller.modalPresentationStyle = UIModalPresentationCustom;
}else{
controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
controller.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentViewController:controller animated:YES completion:nil];
}
关键是设置modalPresentationStyle为UIModalPresentationCustom,然后当presentViewController方法调用时,iOS系统就会创建出UIPresentationController的实例,来控制跳转的行为
分享到:
相关推荐
iOS的UIPresentationController和附带的UIPresentationController提供了固定在屏幕边缘的视图控制器(如动作表)的视图控制器。 安装 迅捷软件包管理器 要将SheetPresentation与,请将其作为依赖项从Xcode中添加到您...
UIPresentationController是 iOS8 新增的一个API,苹果的官方定义是:对象为所呈现的视图控制器提供高级视图的转换管理。这里将使用UIPresentationController实现一些modal动画效果,有详细的注释和讲解,欢迎点赞和...
这似乎是iOS 7或iOS 8上的UIKit错误UIPresentationController通过提供一种在方向更改时调整控制器大小的方法来解决该问题。 状态恢复是可能的,但提出的VC应该恢复transitioningDelegate , ...
iPhone Xs模拟器上的DEMO视频iOS CardPresentationController自定义UIPresentationController中的模态演示,它模仿Apple Music UI的行为。 在iOS 10及更高版本上应该可以正常工作。 iPhone Xs模拟器上的DEMO视频iOS ...
iOS 模态窗口,内置类似淘宝添加购物车的模态视图动画,内部使用 iOS8 推出的UIPresentationController来实现模态窗口功能。
PopView根据ios8.0推出的UIPresentationController写了一个小框架,方便实现相应的弹出视图功能###ps.这个小框架是通过swift 1.2的语法书写的,需要至少xcode6.3.1的支持。###关于Version1.0Version1.0版本 - ...
KYPresentationController iOS8 中新添加的UIPresentationController的Demo 查看博客介绍在这里:
iOS通过自定义UIPresentationController定制下拉框视图和弹出效果;asfasjfjasfljaslkdfjlasjlfjlasjdfljasljfljasldjfljasldjflkjasldfjlas
MZFormSheetPresentationController提供了对本机iOS UIModalPresentationFormSheet的替代,增加了对iPhone的支持以及设置控制器大小和感觉表单的其他机会。 MZFormSheetPresentationController还具有许多预定义的...
PresenterKit iOS的Swifty视图控制器演示文稿关于视图控制器演示文稿,解雇和UIPresentationController的Swifty包装器。 需求Swift 4.0+ Xcode 9+ iOS 9.0+ PresenterKit适用于iOS的自定义演示者和更好的视图控制器...
iOS 13中的模态演示iOS 13改变了普通present(vc, ...) calls -所有模态现在看起来像卡。 因此,您在iOS 13上不需要此库。我始终建议尽可能多地使用系统资源,因此,如果您在iOS 13上,则该库默认情况下会回退到系统...
用UIPresentationController来写一个简洁漂亮的底部弹出控件 iOS App开发过程中,底部弹出框是一个非常常见的需求。...我们需要在iOS8及以上的系统中使用UIPresentationController,使用时需要新建一个类继承UIPresenta
在之前如果想要在iphone上实现popover的效果需要自定义view,在iOS8中提供了UIPopoverPresentationController在ipad和iphone两个设备上同时实现popover的效果。找了找网上具体使用的栗子比较少,加上我也是个小菜鸟,...
源码PresentationControllers,PresentationControllers展示了如何使用UIPresentationController快速创建一个自定义模态视图。更多内容可参考:iOS 8的PresentationController
由于 AnimatedViewController 使用 UIPresentationController 并且是动态框架,因此需要 iOS 8 安装 安装 AnimatedViewController: 下载、克隆或子模块 AnimatedViewController。 将AnimatedViewController....
要求iOS 8和iOS SDK 8.0及更高版本。安装MSTDropDownPresentationController可通过。 要安装它,只需将以下行添加到您的Podfile中: pod "MSTDropDownPresentationController"用法使用...
支持模式演示,UIPresentationController和UINavigationController过渡 1行交互式支持过渡,具有多个平移手势指示 以3个自定义转换为例 iOS和tvOS :mobile_phone_with_arrow: 安装 使用 编辑您的Podfile并指定...
适用于iOS的简单优雅的下拉转换为什么? 我需要在正在构建的应用程序中执行下拉转换,我发现那里有许多很棒的库可以提供所需的功能。 但是它们所有人都有一个缺陷:它们不是作为自定义过渡实现的,而是作为某些视图...
Jelly是一个用于动画,非交互式和交互式ViewController的库过渡和演示,着重于简单而灵活的API。 通过几行源代码,交互式UIViewController进行了转换并可以创建可自定义大小的UIViewController演示文稿, 无需使用...