一个基本的弹出工具包可以让你轻松创建弹出视图。你可以专注于你想要的唯一视图。
此外,它还包括两种常见的弹出视图:MMAlertView 和 MMSheetView。你可以轻松使用和自定义它。
或者,您可以查看下面的演示视频(单击图片)。
首选的安装方式是通过 CocoaPods。只需添加
pod 'MMPopupView'
并运行 pod install
。它将安装 MMPopupView 的最新版本。
如果您想使用 MMPopupView 的最新代码,请使用
pod 'MMPopupView', :head
//MMAlertView
NSArray *items =
@[MMItemMake(@"Done", MMItemTypeNormal, block),
MMItemMake(@"Save", MMItemTypeHighlight, block),
MMItemMake(@"Cancel", MMItemTypeNormal, block)];
[[[MMAlertView alloc] initWithTitle:@"AlertView"
detail:@"each button take one row if there are more than 2 items"
items:items]
showWithBlock:completeBlock];
//MMSheetView
NSArray *items =
@[MMItemMake(@"Normal", MMItemTypeNormal, block),
MMItemMake(@"Highlight", MMItemTypeHighlight, block),
MMItemMake(@"Disabled", MMItemTypeDisabled, block)];
[[[MMSheetView alloc] initWithTitle:@"SheetView"
items:items] showWithBlock:completeBlock];
MMPopupView 是一个基本弹出视图,旨在被继承。
它提供了 3 种动画(alert,sheet,drop),或者你可以通过覆盖 showAnimation 和 hideAnimation 来提供自己的动画。
typedef NS_ENUM(NSUInteger, MMPopupType) {
MMPopupTypeAlert,
MMPopupTypeSheet,
MMPopupTypeCustom,
};
@class MMPopupView;
typedef void(^MMPopupBlock)(MMPopupView *);
typedef void(^MMPopupCompletionBlock)(MMPopupView *, BOOL);
@interface MMPopupView : UIView
@property (nonatomic, assign, readonly) BOOL visible; // default is NO.
@property (nonatomic, strong ) UIView *attachedView; // default is MMPopupWindow. You can attach MMPopupView to any UIView.
@property (nonatomic, assign ) MMPopupType type; // default is MMPopupTypeAlert.
@property (nonatomic, assign ) NSTimeInterval animationDuration; // default is 0.3 sec.
@property (nonatomic, assign ) BOOL withKeyboard; // default is NO. When YES, alert view with be shown with a center offset (only effect with MMPopupTypeAlert).
@property (nonatomic, copy ) MMPopupCompletionBlock showCompletionBlock; // show completion block.
@property (nonatomic, copy ) MMPopupCompletionBlock hideCompletionBlock; // hide completion block
@property (nonatomic, copy ) MMPopupBlock showAnimation; // custom show animation block.
@property (nonatomic, copy ) MMPopupBlock hideAnimation; // custom hide animation block.
/**
* override this method to show the keyboard if with a keyboard
*/
- (void) showKeyboard;
/**
* override this method to hide the keyboard if with a keyboard
*/
- (void) hideKeyboard;
/**
* show the popup view
*/
- (void) show;
/**
* show the popup view with completiom block
*
* @param block show completion block
*/
- (void) showWithBlock:(MMPopupBlock)block;
/**
* hide the popup view
*/
- (void) hide;
/**
* hide the popup view with completiom block
*
* @param block hide completion block
*/
- (void) hideWithBlock:(MMPopupBlock)block;
@end
/**
* hide all popupview with current class, eg. [MMAlertview hideAll];
*/
+ (void) hideAll;
如果您想创建自己的弹出视图,简单地只需要从 MMPopupView 继承。
@interface YourCustomView : MMPopupView
@end
定制后,您可以直接使用它。
[YourCustomView show];
[YourCustomView showWithBlock:completionBlock];
[YourCustomView hide];
[YourCustomView hideWithBlock:completionBlock];
MMAlertView 基于 MMPopupView。
typedef void(^MMPopupInputHandler)(NSString *text);
@interface MMAlertView : MMPopupView
@property (nonatomic, assign) NSUInteger maxInputLength; // default is 0. Means no length limit.
- (instancetype) initWithInputTitle:(NSString*)title
detail:(NSString*)detail
placeholder:(NSString*)inputPlaceholder
handler:(MMPopupInputHandler)inputHandler;
- (instancetype) initWithConfirmTitle:(NSString*)title
detail:(NSString*)detail;
- (instancetype) initWithTitle:(NSString*)title
detail:(NSString*)detail
items:(NSArray*)items;
@end
MMAlertViewConfig 是 MMAlertView 的全局配置,您可以通过调整它来完全自定义。
@interface MMAlertViewConfig : NSObject
+ (MMAlertViewConfig*) globalConfig;
@property (nonatomic, assign) CGFloat width; // Default is 275.
@property (nonatomic, assign) CGFloat buttonHeight; // Default is 50.
@property (nonatomic, assign) CGFloat innerMargin; // Default is 25.
@property (nonatomic, assign) CGFloat cornerRadius; // Default is 5.
@property (nonatomic, assign) CGFloat titleFontSize; // Default is 18.
@property (nonatomic, assign) CGFloat detailFontSize; // Default is 14.
@property (nonatomic, assign) CGFloat buttonFontSize; // Default is 17.
@property (nonatomic, strong) UIColor *backgroundColor; // Default is #FFFFFF.
@property (nonatomic, strong) UIColor *titleColor; // Default is #333333.
@property (nonatomic, strong) UIColor *detailColor; // Default is #333333.
@property (nonatomic, strong) UIColor *splitColor; // Default is #CCCCCC.
@property (nonatomic, strong) UIColor *itemNormalColor; // Default is #333333. effect with MMItemTypeNormal
@property (nonatomic, strong) UIColor *itemHighlightColor; // Default is #E76153. effect with MMItemTypeHighlight
@property (nonatomic, strong) UIColor *itemPressedColor; // Default is #EFEDE7.
@property (nonatomic, strong) NSString *defaultTextOK; // Default is "好".
@property (nonatomic, strong) NSString *defaultTextCancel; // Default is "取消".
@property (nonatomic, strong) NSString *defaultTextConfirm; // Default is "确定".
@end
MMSheetView 基于 MMPopupView。
@interface MMSheetView : MMPopupView
- (instancetype) initWithTitle:(NSString*)title
items:(NSArray*)items;
@end
MMSheetViewConfig 是 MMSheetView 的全局配置,您可以通过调整它来完全自定义。
@interface MMSheetViewConfig : NSObject
+ (MMSheetViewConfig*) globalConfig;
@property (nonatomic, assign) CGFloat buttonHeight; // Default is 50.
@property (nonatomic, assign) CGFloat innerMargin; // Default is 19.
@property (nonatomic, assign) CGFloat titleFontSize; // Default is 14.
@property (nonatomic, assign) CGFloat buttonFontSize; // Default is 17.
@property (nonatomic, strong) UIColor *backgroundColor; // Default is #FFFFFF.
@property (nonatomic, strong) UIColor *titleColor; // Default is #666666.
@property (nonatomic, strong) UIColor *splitColor; // Default is #CCCCCC.
@property (nonatomic, strong) UIColor *itemNormalColor; // Default is #333333. effect with MMItemTypeNormal
@property (nonatomic, strong) UIColor *itemDisableColor; // Default is #CCCCCC. effect with MMItemTypeDisabled
@property (nonatomic, strong) UIColor *itemHighlightColor; // Default is #E76153. effect with MMItemTypeHighlight
@property (nonatomic, strong) UIColor *itemPressedColor; // Default is #EFEDE7.
@property (nonatomic, strong) NSString *defaultTextCancel; // Default is "取消"
@end
v1.7.1 修复了当 attachView 是主 keyWindow 时出现的黑屏问题。
v1.7 添加了模糊效果。
@interface UIView (MMPopup)
@property (nonatomic, strong, readonly ) UIView *mm_dimBackgroundBlurView;
@property (nonatomic, assign ) BOOL mm_dimBackgroundBlurEnabled;
@property (nonatomic, assign ) UIBlurEffectStyle mm_dimBackgroundBlurEffectStyle;
@end
例如。
alertView.attachedView = self.view;
alertView.attachedView.mm_dimBackgroundBlurEnabled = YES;
alertView.attachedView.mm_dimBackgroundBlurEffectStyle = UIBlurEffectStyleLight;
v1.6 添加 '+ hideAll' 方法,改进代码结构。
v1.5.3 修复了 touchWildToHide
的触摸问题。
v1.5.2 修复了在自定义视图中存在 scrollviews 时的触摸问题。
v1.5.1 修复了显示问题。
v1.5 修复了旋转问题。
v1.4 调整了动画缓动函数。重建演示。(感谢 @yoavlt)
v1.3 修复了错误。
v1.2 现在,您可以使用 isVisible
判断 MMPopupView 是否可见。
@property (nonatomic, assign, readonly) BOOL visible; // default is NO.
v1.1 现在,您可以使用 attach
将 MMPopupView 附加到任何您想要的自定义视图。
@property (nonatomic, strong ) UIView *attachedView; // default is MMPopupWindow. You can attach MMPopupView to any UIView.
v1.0 第一个版本