服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 简单好用可任意定制的iOS Popover气泡效果

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

2021-04-12 11:00Assuner IOS

Popover(气泡弹出框/弹出式气泡/气泡)是由一个矩形和三角箭头组成的弹出窗口,箭头指向的地方通常是导致Popover弹出的控件或区域。本文通过实例代码给大家介绍了iOS Popover气泡效果,需要的朋友参考下吧

效果图如下所示:

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

swift: https://github.com/corin8823/popover oc: https://github.com/assuner-lee/popoverobjc

使用示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- (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里设置。

弹出气泡

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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, 偏移;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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气泡效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家的支持!

原文链接:https://juejin.im/post/5a30902c5188253e2470f4f7?utm_source=tuicool&utm_medium=referral

延伸 · 阅读

精彩推荐
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22