基本的弹出视图工具包允许你轻松创建弹出视图。你可以专注于仅显示你想要展示的视图。
此外,它还包含2个常见的弹出视图,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),或者你也可以通过覆盖strong showAnimation 和 strong 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;
如果你想要创建自己的弹出视图,简单地从 strong MMPopupView 派生子类。
@interface YourCustomView : MMPopupView
@end
在自定义后,你可以简单地使用它。
[YourCustomView show];
[YourCustomView showWithBlock:completionBlock];
[YourCustomView hide];
[YourCustomView hideWithBlock:completionBlock];
MMAlertView基于 strong 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是 strong 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基于 strong MMPopupView。
@interface MMSheetView : MMPopupView
- (instancetype) initWithTitle:(NSString*)title
items:(NSArray*)items;
@end
MMSheetViewConfig是 strong MMAlertView 的全局配置,你可以通过调整它进行完全自定义。
@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 是 main 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 修复了与 strong touchWildToHide 相关的触摸问题。
v1.5.2 修复了在自定义视图中存在滚动视图时的触摸问题。
v1.5.1 修复了显示问题。
v1.5 修复了旋转问题。
v1.4 调整了动画缓动函数。重建了示例。(感谢 @yoavlt)
v1.3 修复了错误。
v1.2 现在你可以通过使用来了解MMPopupView是否可见
@property (nonatomic, assign, readonly) BOOL visible; // default is NO.
v1.1 现在你可以通过使用来将MMPopupView附加到任何你想要的 UIView
@property (nonatomic, strong ) UIView *attachedView; // default is MMPopupWindow. You can attach MMPopupView to any UIView.
v1.0 第一个版本