1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > iOS 模态窗口居中弹出背景半透明

iOS 模态窗口居中弹出背景半透明

时间:2024-04-05 02:43:31

相关推荐

iOS 模态窗口居中弹出背景半透明

实现功能:点击UIImageView弹出一个模态窗口并居中显示,背景为灰色半透明

App中有很多地方用到点击用户头像并弹出一个模态窗体,显示该用户的一些信息,考虑到很多地方使用,就写了一个UIImageView的类别,将点击手势封装到这个类别中,已达到共用代码的效果。github上有很多模态窗体库

实现思路:先将当前视图控制器的视图设置为透明[self.view.backgroundColor clearColor],然后将一个灰色半透明UIView(grayAlphaView)添加到self.view

, [self.view addSubview:grayAlphaView]; 再将一个容器视图containerView(UIView) 添加到灰色半透明视图上grayAlphaView

[grayAlphaView addSubview:containerView];

1.UIImageView+UserHeadTap

@interface UIImageView (UserHeadTap)- (id) initWithTap;@end

#import "UIImageView+UserHeadTap.h"#import "NameCardViewController.h"#import "UIViewController+Utils.h"@implementation UIImageView (UserHeadTap)- (id) initWithTap {if (self = [super init]) {self.userInteractionEnabled = YES;UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userHeaderTapAction:)];[self addGestureRecognizer:tapGestureRecognizer];}return self;}- (void) userHeaderTapAction:(UITapGestureRecognizer *)tapGestureRecognizer {UIViewController * parentViewController = [UIViewController currentViewController];NameCardViewController * controller = [[NameCardViewController alloc] init];controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {controller.providesPresentationContextTransitionStyle = YES;controller.definesPresentationContext = YES;controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;[parentViewController presentViewController:controller animated:YES completion:nil];} else {parentViewController.view.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;[parentViewController presentViewController:controller animated:NO completion:nil];parentViewController.view.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;}}@end

2.ViewController

#import "UIViewController+Utils.h"@implement ViewController - (void)viewDidLoad {UIImageView * userHeadImageView = [UIImageView initWithTap];userHeadImageView.frmae = CGRectMake(50, 50, 70, 70);userHeadImageView.image = [UIImage imageNamed:@"icon"];[self.view addSubview:userHeadImageView];}@end

3. NameCardViewController(名片)

#import "NameCardViewController.h"@interface NameCardViewController ()@end

@implementation NameCardViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor clearColor];UIView *grayAlphaView = [[UIView alloc] initWithFrame:self.view.frame];grayAlphaView.backgroundColor = [UIColor grayColor];grayAlphaView.alpha = 0.8;[self.view addSubview:grayAlphaView];UIView *containerView = [[UIView alloc] init];containerView.size = CGSizeMake(self.view.size.width - 40 * 2, 350);containerView.center = self.view.center;containerView.backgroundColor = [UIColor redColor];[grayAlphaView addSubview:containerView];}@end

4. 实现效果如果

该效果还没有实现全屏

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。