简单好用可任意定制的iOS Popover气泡效果

 更新时间:2017年12月22日 11:46:54   作者:Assuner  
Popover(气泡弹出框/弹出式气泡/气泡)是由一个矩形和三角箭头组成的弹出窗口,箭头指向的地方通常是导致Popover弹出的控件或区域。本文通过实例代码给大家介绍了iOS Popover气泡效果,需要的朋友参考下吧

效果图如下所示:

 

swift: https://github.com/corin8823/Popover OC: https://github.com/Assuner-Lee/PopoverObjC

使用示例

pod 'PopoverObjC'
#import "ASViewController.h"
#import <PopoverObjC/ASPopover.h>
@interface ASViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (nonatomic, strong) ASPopover *btnPopover;
@property (nonatomic, strong) ASPopover *itemPopover;
@end
@implementation ASViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 [self.btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"item" style:UIBarButtonItemStylePlain target:self action:@selector(clickItem:)];
}
- (void)didReceiveMemoryWarning {
}

初始化Popover

- (ASPopover *)btnPopover {
 if (!_btnPopover) {
 ASPopoverOption *option = [[ASPopoverOption alloc] init];
 option.popoverType = ASPopoverTypeUp;
 option.autoAjustDirection = NO;
 option.arrowSize = CGSizeMake(9, 6);
 option.blackOverlayColor = [UIColor clearColor];
 option.popoverColor = [UIColor lightGrayColor];
 option.dismissOnBlackOverlayTap = YES;
 option.animationIn = 0.5;
 //...
 _btnPopover = [[ASPopover alloc] initWithOption:option];
 }
 return _btnPopover;
}
- (ASPopover *)itemPopover {
 if (!_itemPopover) {
 ASPopoverOption *option = [[ASPopoverOption alloc] init];
 option.autoAjustDirection = NO;
 option.arrowSize = CGSizeMake(10, 6);
 option.blackOverlayColor = [UIColor clearColor];
 option.sideEdge = 7;
 option.dismissOnBlackOverlayTap = YES;
 option.popoverColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
 option.autoAjustDirection = YES;
 option.animationIn = 0.4;
 option.springDamping = 0.5;
 option.initialSpringVelocity = 1;
 option.overlayBlur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
 //...
 _itemPopover = [[ASPopover alloc] initWithOption:option];
 }
 return _itemPopover;
}

popover的属性可在option里设置。

弹出气泡

- (void)clickBtn:(id)sender {
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 50, 40)];
 [self.btnPopover show:view fromView:self.btn]; // in delegate window
}
- (void)clickItem:(id)sender {
 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
 UIView *itemView = [self.navigationItem.rightBarButtonItem valueForKey:@"view"]; // you should use custom view in item;
 if (itemView) {
// [self.itemPopover show:view fromView:itemView];
 CGPoint originPoint = [self.itemPopover originArrowPointWithView:view fromView:itemView];
 originPoint.y += 5;
 [self.itemPopover show:view atPoint:originPoint];
 }
}
@end

可在某一个视图或某一个point上弹出内容view

Popover interface
#import <UIKit/UIKit.h>
#import "ASPopoverOption.h"
typedef void (^ASPopoverBlock)(void);
@interface ASPopover : UIView
@property (nonatomic, copy) ASPopoverBlock willShowHandler;
@property (nonatomic, copy) ASPopoverBlock willDismissHandler;
@property (nonatomic, copy) ASPopoverBlock didShowHandler;
@property (nonatomic, copy) ASPopoverBlock didDismissHandler;
@property (nonatomic, strong) ASPopoverOption *option;
- (instancetype)initWithOption:(ASPopoverOption *)option;
- (void)dismiss;
- (void)show:(UIView *)contentView fromView:(UIView *)fromView;
- (void)show:(UIView *)contentView fromView:(UIView *)fromView inView:(UIView *)inView;
- (void)show:(UIView *)contentView atPoint:(CGPoint)point;
- (void)show:(UIView *)contentView atPoint:(CGPoint)point inView:(UIView *)inView;
- (CGPoint)originArrowPointWithView:(UIView *)contentView fromView:(UIView *)fromView;
- (CGPoint)arrowPointWithView:(UIView *)contentView fromView:(UIView *)fromView inView:(UIView *)inView popoverType:(ASPopoverType)type;
@end

contentView: 要显示的内容; fromView: 气泡从某一个视图上show; inview: 气泡绘制在某一个视图上,一般为delegate window; atPoint: 气泡从某一点上show; 可先获取originPoint, 偏移;

PopoverOption Interface
typedef NS_ENUM(NSInteger, ASPopoverType) {
 ASPopoverTypeUp = 0,
 ASPopoverTypeDown,
};
@interface ASPopoverOption : NSObject
@property (nonatomic, assign) CGSize arrowSize;
@property (nonatomic, assign) NSTimeInterval animationIn; // if 0, no animation
@property (nonatomic, assign) NSTimeInterval animationOut;
@property (nonatomic, assign) CGFloat cornerRadius;
@property (nonatomic, assign) CGFloat sideEdge;
@property (nonatomic, strong) UIColor *blackOverlayColor;
@property (nonatomic, strong) UIBlurEffect *overlayBlur;
@property (nonatomic, strong) UIColor *popoverColor;
@property (nonatomic, assign) BOOL dismissOnBlackOverlayTap;
@property (nonatomic, assign) BOOL showBlackOverlay;
@property (nonatomic, assign) CGFloat springDamping;
@property (nonatomic, assign) CGFloat initialSpringVelocity;
@property (nonatomic, assign) ASPopoverType popoverType;
@property (nonatomic, assign) BOOL highlightFromView;
@property (nonatomic, assign) CGFloat highlightCornerRadius;
@property (nonatomic, assign) BOOL autoAjustDirection; // down preferred, effect just in view not at point
@end

总结

以上所述是小编给大家介绍的简单好用可任意定制的iOS Popover气泡效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家的支持!

相关文章

  • 浅谈IOS如何对app进行安全加固

    浅谈IOS如何对app进行安全加固

    运行在越狱设备上的IOS app,非常容易遭到破解分析,这里列举一些可以加大破解难度的方法,希望有所帮助。
    2021-06-06
  • iOS Swift控制器转场动画示例代码

    iOS Swift控制器转场动画示例代码

    这篇文章主要给大家介绍了关于iOS Swift控制器转场动画的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-01-01
  • iOS实现手势解锁操作

    iOS实现手势解锁操作

    这篇文章主要为大家详细介绍了iOS实现手势解锁操作功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • iOS拨打电话的3种实现方式

    iOS拨打电话的3种实现方式

    这篇文章主要介绍了iOS拨打电话的3种实现方式 ,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-06-06
  • 高仿IOS的Android弹出框

    高仿IOS的Android弹出框

    这篇文章主要为大家详细介绍了高仿IOS的Android弹出框的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • iOS StoreKit 2 新特性盘点解析

    iOS StoreKit 2 新特性盘点解析

    这篇文章主要为大家介绍了iOS StoreKit 2 新特性盘点及要点解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • Drawer Builder组件实现flutter侧边抽屉效果示例分析

    Drawer Builder组件实现flutter侧边抽屉效果示例分析

    这篇文章主要为大家介绍了Drawer Builder组件实现flutter侧边抽屉效果示例分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • IOS 中UITextField,UITextView,UILabel 根据内容来计算高度

    IOS 中UITextField,UITextView,UILabel 根据内容来计算高度

    这篇文章主要介绍了IOS 中UITextField,UITextView,UILabel 根据内容来计算高度的相关资料,需要的朋友可以参考下
    2017-03-03
  • IOS 开发之网络图片轮播图的实现

    IOS 开发之网络图片轮播图的实现

    这篇文章主要介绍了IOS 开发之网络图片轮播图的实现的相关资料,希望通过此文大家能够掌握轮播图的实现,需要的朋友可以参考下
    2017-09-09
  • iOS中字符串换行的实现方法

    iOS中字符串换行的实现方法

    大家应该都有所体会,单行字符数过多会影响美观,所以下面这篇文章主要给大家介绍了关于iOS中字符串换行的实现方法,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2018-07-07

最新评论