UIPopoverPresentationController 的初次使用

今天在聊某个需求的时候,我们组长发现我在实现下面这种效果时感觉很奇怪,我当时是直接画上去的,然而...

效果图

说为什么要用自己去画,非常鄙视的说我,咋不用系统的 UIPopoverPresentationController ,😆😆😆


之前关那个背景图画的...

#pragma mark - DrawRect
- (void)drawRect:(CGRect)rect {
    CGRect frame = CGRectMake(0, kArrowHeight, self.bounds.size.width, self.bounds.size.height-kArrowHeight);
    // 获取各个角落点
    float xMin = CGRectGetMinX(frame);
    float yMin = CGRectGetMinY(frame);
    float xMax = CGRectGetMaxX(frame);
    float yMax = CGRectGetMaxY(frame);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineCapStyle = kCGLineCapRound; //线条拐角
    path.lineJoinStyle = kCGLineCapRound; //终点处理
    
    // 左上 圆角构成
    [path moveToPoint:CGPointMake(xMin, yMin + kCornerRadius)];
    [path addQuadCurveToPoint:CGPointMake(xMin + kCornerRadius, yMin) controlPoint:CGPointMake(xMin, yMin)];
    
    // 箭头
    // 获取箭头point 
    CGPoint arrowPoint = CGPointMake(self.bounds.size.width - 2 *kArrowHeight, 0);
    // 箭头的构成
    [path addLineToPoint:CGPointMake(arrowPoint.x - kArrowRadius, yMin)];
    [path addLineToPoint:arrowPoint];
    [path addLineToPoint:CGPointMake(arrowPoint.x + kArrowRadius, yMin)];
    
    // 右上 圆角构成
    [path addLineToPoint:CGPointMake(xMax - kCornerRadius, yMin)];
    [path addQuadCurveToPoint:CGPointMake(xMax, yMin + kCornerRadius) controlPoint:CGPointMake(xMax, yMin)];
    
    // 右下 圆角构成
    [path addLineToPoint:CGPointMake(xMax, yMax - kCornerRadius)];
    [path addQuadCurveToPoint:CGPointMake(xMax - kCornerRadius, yMax) controlPoint:CGPointMake(xMax, yMax)];

    // 左下 圆角构成
    [path addLineToPoint:CGPointMake(xMin + kCornerRadius, yMax)];
    [path addQuadCurveToPoint:CGPointMake(xMin, yMax - kCornerRadius) controlPoint:CGPointMake(xMin, yMax)];
    // 填充颜色
    [self.mainColor setFill];
    [path fill];
    [path closePath];
}


之前一直知道这个,但一直没用过,于是今天稍微了解下,并尝试小小实现下。

  • 使用测试:
- (void)buttonAction:(UIButton *)sender{
    
    TestViewController *testVC = [[TestViewController alloc] init];
    // 设置大小
    testVC.preferredContentSize = CGSizeMake(150, 300);
    // 设置 Sytle
    testVC.modalPresentationStyle = UIModalPresentationPopover;
    // 需要通过 sourceView 来判断位置的
    testVC.popoverPresentationController.sourceView = self.button;
    // 指定箭头所指区域的矩形框范围(位置和尺寸),以sourceView的左上角为坐标原点
    // 这个可以 通过 Point 或  Size 调试位置
    testVC.popoverPresentationController.sourceRect = self.button.bounds;
    // 箭头方向
    testVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    // 设置代理
    testVC.popoverPresentationController.delegate = self;
    [self presentViewController:testVC animated:YES completion:nil];
}
  • UIPopoverPresentationControllerDelegate 的代理
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationNone; //不适配
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
    return YES;   //点击蒙版popover消失, 默认YES
}
  • barButtonItem 的方便:
    实际上就是其 API 中另外一个属性:
@property (nullable, nonatomic, strong) UIBarButtonItem *barButtonItem;

如果有navigationController, 并且从right/leftBarButtonItem点击后出现popover, 则可以把 right/leftBarButtonItem看做上面说的sourceView.默认箭头指向up,所以在这种情况下也可以不设置箭头方向。

  • 一个 Bug
[Warning] <_UIPopoverBackgroundVisualEffectView 0x7fafbae14830> is being asked to animate its opacity. 
This will cause the effect to appear broken until opacity returns to 1.

话说这个问题是系统在执行alpha渐变动画时,UIVisualEffectView创建了裁剪区域外的模糊实例,警告说,他的效果可能会被破坏,然而经过测试没有被破坏。 暂时可以不做处理?

对于目前我们项目中已经支持 iOS 8 的来说,直接用UIPopoverPresentationController 是很方便的, 而且系统的肯定更好些,之前画的确实有点挫,想着是否要换呢。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容