博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单好用可任意定制的iOS Popover气泡
阅读量:6373 次
发布时间:2019-06-23

本文共 5641 字,大约阅读时间需要 18 分钟。

先看效果

使用示例

pod 'PopoverObjC'复制代码
#import "ASViewController.h"#import 
@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.autoAjustDirection = YES;    option.preferedType = ASPopoverTypeDown;    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 = YES;    option.arrowSize = CGSizeMake(10, 6);    option.offset = 3;  // vertical offset from original show point, default is 0.    option.blackOverlayColor = [UIColor clearColor];    option.sideEdge = 7;    option.dismissOnBlackOverlayTap = YES;    option.popoverColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];    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]; // above, option has contained offset value; invoke [self show:contentView fromView:fromView inView:keyWindow];        /* other method to show          CGPoint orignPoint = [self.itemPopover orignArrowPointWithView:view fromView:itemView];     orignPoint.y += 5; // offset in option has same effect     [self.itemPopover show:view atPoint:orignPoint];          */  }}复制代码

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

Popover interface

#import 
#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

#import 
typedef NS_ENUM(NSInteger, ASPopoverType) { ASPopoverTypeUp = 0, ASPopoverTypeDown,};@interface ASPopoverOption : NSObject@property (nonatomic, assign) CGSize arrowSize; // default is (10, 7)@property (nonatomic, assign) CGFloat offset; // vertical offset from original show point, default is 0.@property (nonatomic, strong) UIColor *popoverColor; // contain view color. including arrow.@property (nonatomic, assign) BOOL autoAjustDirection; //effect just in view not at point; default is YES.@property (nonatomic, assign) ASPopoverType preferedType; // just effect when autoAjustDirection = YES; default is ASPopoverTypeUp;@property (nonatomic, assign) ASPopoverType popoverType; // default is ASPopoverTypeUp; not effect when autoAjustDirection = YES;@property (nonatomic, assign) NSTimeInterval animationIn; // if 0, no animation; default is 0.6.@property (nonatomic, assign) NSTimeInterval animationOut; // if 0, no animation; default is 0.3.@property (nonatomic, assign) CGFloat springDamping;@property (nonatomic, assign) CGFloat initialSpringVelocity;@property (nonatomic, assign) CGFloat cornerRadius;@property (nonatomic, assign) CGFloat sideEdge;@property (nonatomic, strong) UIColor *blackOverlayColor; // default is black/alpha 0.2, can be clear color.@property (nonatomic, strong) UIBlurEffect *overlayBlur;@property (nonatomic, assign) BOOL dismissOnBlackOverlayTap; // default is YES.@property (nonatomic, assign) BOOL showBlackOverlay; // default is YES.@property (nonatomic, assign) BOOL highlightFromView;@property (nonatomic, assign) CGFloat highlightCornerRadius;@end复制代码

多谢观看

转载地址:http://rznqa.baihongyu.com/

你可能感兴趣的文章
获取设备列表
查看>>
项目软件集成三方模块,编译中int32和uint32定义冲突解决方法
查看>>
StretchDIBits速度测试(HALFTONE)
查看>>
在.NET Workflo“.NET研究”w 3.5中使用多线程提高工作流性能
查看>>
验证Oracle处理速度
查看>>
Flink - NetworkEnvironment
查看>>
BZOJ4374 : Little Elephant and Boxes
查看>>
javascript类型系统——包装对象
查看>>
springcloud(十三):Eureka 2.X 停止开发,但注册中心还有更多选择:Consul 使用详解...
查看>>
IdHttpServer实现webservice
查看>>
爱立信物联网广州路演
查看>>
Stopping and/or Restarting an embedded Jetty in...
查看>>
多线程核对MD5码脚本
查看>>
【计数】【UVA11401】 Triangle Counting
查看>>
[android] ndk环境的搭建
查看>>
Namespacing in PHP (php 中使用命名空间)
查看>>
GCD之并行串行区别
查看>>
Xshell用鼠标选中一段文字后自动换行的问题
查看>>
vue-element-admin 4.0.1 发布,后台集成方案
查看>>
sql左链接、内链接、右链接、全链接
查看>>