版本记录
| 版本号 | 时间 |
|---|---|
| V1.0 | 2017.05.17 |
前言
我们app中经常用到pop动画,比较常见的比如微信和QQ的加人等,这些都是pop动画,oc中有原生的pop动画类,这里我先给大家举一个非常简单的例子,非常简单几行代码就可以搞定。
详细
我们先看一下代码结构

代码结构
下面我们就直接看代码吧。
1. AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
JJPopoverVC *popVC = [[JJPopoverVC alloc] init];
self.window.rootViewController = popVC;
[self.window makeKeyAndVisible];
return YES;
}
2. JJPopoverVC.h
#import <UIKit/UIKit.h>
@interface JJPopoverVC : UIViewController
@end
3. JJPopoverVC.m
#import "JJPopoverVC.h"
#import "JJSecondVC.h"
@interface JJPopoverVC () <UIPopoverPresentationControllerDelegate>
@property (nonatomic, strong) UIButton *addButton;
@end
@implementation JJPopoverVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//pop动画按钮
UIButton *popButton = [UIButton buttonWithType:UIButtonTypeCustom];
[popButton setTitle:@"pop动画" forState:UIControlStateNormal];
[popButton addTarget:self action:@selector(popoverAnimation:) forControlEvents:UIControlEventTouchUpInside];
popButton.center = self.view.center;
[popButton sizeToFit];
popButton.backgroundColor = [UIColor magentaColor];
[self.view addSubview:popButton];
//调到下个界面按钮
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[addButton addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.addButton = addButton;
addButton.frame = CGRectMake(200, self.view.bounds.origin.y, 100, 100);
[self.view addSubview:addButton];
}
#pragma mark - Action && Notification
- (void)popoverAnimation:(UIButton *)button
{
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor greenColor];
vc.modalPresentationStyle = UIModalPresentationPopover;
//设置弹窗控制器视图的大小
vc.preferredContentSize = CGSizeMake(0, 120);
vc.popoverPresentationController.delegate = self;
//设置定位控件
vc.popoverPresentationController.sourceView = button;
//设置箭头方向向上
vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
//设置箭头的位置,原点可以参考某一个控件的尺寸设置,宽高通常用于设置附加的偏移量,通常传入0即可
CGSize size = button.bounds.size;
vc.popoverPresentationController.sourceRect = CGRectMake(size.width * 0.5, size.height, 0, 0);
//设置绕开控件,注意:同一时间只能允许展现一个控制器,这个属性是设置当UIPopoverController显示出来的时候,哪些控件可以继续跟用户进行正常交互,点击区域外的控件就不会让UIPopoverController消失了。
// vc.popoverPresentationController.passthroughViews = @[self.addButton];//绕开-按钮可以交互
vc.popoverPresentationController.passthroughViews = nil;//不绕开-点击按钮pop消失,按钮不能交互
//展现模态控制器
[self presentViewController:vc animated:YES completion:nil];
}
- (void)addButtonClick:(UIButton *)button
{
JJSecondVC *secondVC = [[JJSecondVC alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
}
#pragma mark - UIPopoverPresentationControllerDelegate
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
//不使用系统样式
return UIModalPresentationNone;
}
@end
这里有个参数需要说一下:vc.popoverPresentationController.passthroughViews
字面意思就是绕开的意思,但是如果真正理解的话,最好写个demo亲测一下就会很清楚了。后面需要传递一个数组参数,我们也可以给nil,就表示不绕开任何视图。
- 当传递数组的时候,我这里传递的是@[self.addButton],表示的意思就是绕开这个按钮,也就是说这个按钮还可以和用户进行交互,同时点击这个按钮不会让popover动画隐藏回去。可以看一下下面的gif图就很清楚了。

绕开
可见这里点击按钮,pop动画并不收缩回去,但是也没控制台打印出了下面这些:
2017-05-17 00:21:23.920 popover[1873:42346] Warning: Attempt to present <JJSecondVC: 0x7fa8b360c170> on <JJPopoverVC: 0x7fa8b3507760> which is already presenting <UIViewController: 0x7fa8b350cc80>
2017-05-17 00:21:24.121 popover[1873:42346] Warning: Attempt to present <JJSecondVC: 0x7fa8b3427b20> on <JJPopoverVC: 0x7fa8b3507760> which is already presenting <UIViewController: 0x7fa8b350cc80>
2017-05-17 00:21:24.361 popover[1873:42346] Warning: Attempt to present <JJSecondVC: 0x7fa8b360bd60> on <JJPopoverVC: 0x7fa8b3507760> which is already presenting <UIViewController: 0x7fa8b350cc80>
2017-05-17 00:21:24.577 popover[1873:42346] Warning: Attempt to present <JJSecondVC: 0x7fa8b6018dc0> on <JJPopoverVC: 0x7fa8b3507760> which is already presenting <UIViewController: 0x7fa8b350cc80>
2017-05-17 00:21:24.756 popover[1873:42346] Warning: Attempt to present <JJSecondVC: 0x7fa8b60190c0> on <JJPopoverVC: 0x7fa8b3507760> which is already presenting <UIViewController: 0x7fa8b350cc80>
这个输出的意思就是pop动画已经存在一个了,点击button响应的事件还是展现另外一个控制器。这是不允许的,因为同一时间只允许存在一个。
- 当传递值为nil的时候,表示不会绕开任何控件,也就是说按钮不参与交互,点击按钮和点击其他背景一样,pop动画都会隐藏回去,可以看下面这个gif图。

不绕开
后记
今天就先写个简单的,后面还会介绍更复杂更有趣的私人定制popover,未完,待续~~~

宇宙之美