gpt4 book ai didi

ios - 模仿背景变暗的通知 UIVIew

转载 作者:行者123 更新时间:2023-11-29 12:48:46 25 4
gpt4 key购买 nike

我有兴趣创建一个类似于下面看到的设计模式的弹出式通知 View ,其中 UIView 弹出并且屏幕的其余部分变暗,并且在不使用新模式的情况下完成看法。由于这种设计模式很常见,我想知道是否有一种“行之有效”的方法来做到这一点(或者有一个广泛使用的开源框架,如 AFNetworking 用于下载/上传文件和图像)?

谢谢。

enter image description here enter image description here

最佳答案

这是一个好的开始:

弹出 View .h

#import <UIKit/UIKit.h>

@interface PopupView : NSObject

+ (void)addSubview:(UIView *)view;
+ (void)show;
+ (void)hide;

@end

弹出 View .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];

当然,您可以放置​​任何您想要的 View 而不是标签。

关于ios - 模仿背景变暗的通知 UIVIew,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22950708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com