我有兴趣创建一个弹出式通知视图,类似于下面看到的设计模式,其中弹出UIView
,屏幕的其余部分变暗,并且不使用新的模态视图。由于这种设计模式如此常见,我想知道是否有“经过验证的”方法(或者有一个广泛使用的开源框架,如AFNetworking下载/上传文件和图像)?
感谢。
答案 0 :(得分:1)
这是一个好的开始:
PopupView.h
#import <UIKit/UIKit.h>
@interface PopupView : NSObject
+ (void)addSubview:(UIView *)view;
+ (void)show;
+ (void)hide;
@end
PopupView.m:
#import "PopupView.h"
@implementation PopupView
+(UIView *)sharedView {
static UIView *_view = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
_view.backgroundColor = [UIColor blackColor];
_view.alpha = 0.6;
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
[_view addGestureRecognizer:singleFingerTap];
});
return _view;
}
+ (void)addSubview:(UIView *)view {
UIView *v = [PopupView sharedView];
[v addSubview:view];
}
+ (void)show {
UIView *v = [PopupView sharedView];
v.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
[[appDelegate window] addSubview:v];
}
+ (void)hide {
UIView *v = [PopupView sharedView];
[v removeFromSuperview];
[v.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
}
用法:
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 21)];
l.text = @"Hello";
l.textColor = [UIColor whiteColor];
[PopupView addSubview:l];
[PopupView show];
或者当然不是标签,你可以放任何你想要的视图。