PopupKit 是一个简单灵活的 iOS 框架,用于将任何自定义视图以弹出窗口的形式显示。它包括多种选项来控制弹出窗口的显示和行为。
要导入框架,你可以以下方式之一
import PopupKit
@import PopupKit;
或者
#import <PopupKit/PopupView.h>
使用默认动画和行为(与 UIAlertView 类似)创建用于显示 UIView 的弹出窗口
+ (instancetype)popupWithContentView:(UIView*)contentView;
或者类似地在 Swift 中
convenience init(contentView: UIView)
或者创建一个具有自定义动画和行为的弹出窗口。也可以通过弹出窗口实例上的属性访问自定义设置
+ (instancetype)popupViewWithContentView:(UIView *)contentView
showType:(PopupViewShowType)showType
dismissType:(PopupViewDismissType)dismissType
maskType:(PopupViewMaskType)maskType
shouldDismissOnBackgroundTouch:(BOOL)shouldDismissOnBackgroundTouch
shouldDismissOnContentTouch:(BOOL)shouldDismissOnContentTouch;
或者类似地在 Swift 中
convenience init(contentView: UIView, showType: PopupView.ShowType, dismissType: PopupView.DismissType, maskType: PopupView.MaskType, shouldDismissOnBackgroundTouch: Bool, shouldDismissOnContentTouch: Bool)
另外,在显示之前必须给 contentView
一个大小(通过设置它的帧),或者它必须与 AutoLayout 一起设置大小。
在屏幕中间显示弹出窗口。
- (void)show;
或者类似地在 Swift 中
func show()
有两种方法可以控制弹出窗口的显示位置
PopupView.h
以获取选项)。- (void)showWithLayout:(PopupViewLayout)layout;
或者类似地在 Swift 中
func show(with layout: PopupView.Layout)
- (void)showAtCenter:(CGPoint)center inView:(UIView *)view;
或者类似地在 Swift 中
func show(at center: CGPoint, in view: UIView)
如果你想让弹出窗口自动消失(像 Android 中的 toast 一样),你可以设置一个显式的 duration
- (void)showWithDuration:(NSTimeInterval)duration;
或者类似地在 Swift 中
func show(with duration: TimeInterval)
有多种方法可以消失弹出窗口
如果你有一个弹出窗口实例的引用,你可以向它发送这个消息。如果 animated
,那么它将使用在 dismissType
中指定的动画。否则,它将直接消失
- (void)dismiss:(BOOL)animated;
或者类似地在 Swift 中
func dismiss(animated: Bool)
如果你丢失了对弹出窗口的引用或者你想确保没有弹出窗口在显示,这个类方法可以关闭应用中的所有弹出窗口
+ (void)dismissAllPopups;
或者类似地在 Swift 中
class func dismissAllPopups()
你还可以在 UIView(PopupView)
上调用这个分类方法,在你的 contentView
或它的任何子视图中,以关闭其父级弹出窗口
- (void)dismissPresentingPopup; // UIView category
或者类似地在 Swift 中
func dismissPresentingPopup()
用于显示弹出窗口的动画
@property (nonatomic, assign) PopupViewShowType showType;
关闭弹出窗口时使用的动画
@property (nonatomic, assign) PopupViewDismissType dismissType;
遮罩防止背景的触摸传递到下面的视图
@property (nonatomic, assign) PopupViewMaskType maskType;
当触摸背景时,弹出窗口将自动消失
@property (nonatomic, assign) BOOL shouldDismissOnBackgroundTouch;
当触摸内容视图时,弹出窗口将自动消失
@property (nonatomic, assign) BOOL shouldDismissOnContentTouch;
覆盖半透明背景遮罩的alpha值
@property (nonatomic, assign) CGFloat dimmedMaskAlpha;
使用这些块来同步其他操作与弹出窗口事件
@property (nonatomic, copy) void (^didFinishShowingCompletion)();
@property (nonatomic, copy) void (^willStartDismissingCompletion)();
@property (nonatomic, copy) void (^didFinishDismissingCompletion)();
UIView* contentView = [[UIView alloc] init];
contentView.backgroundColor = [UIColor orangeColor];
contentView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0);
PopupView* popup = [PopupView popupWithContentView:contentView];
[popup show];
KLCPopup是由Jeff Mascia和Kullect团队创建的,在Shout Photo Messenger应用中使用。KLCPopup的一些方面受到了Sam Vermette的SVProgressHUD的启发。PopupKit是由Ryne Cheow移植的KLCPopup的现代版本。