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

来源:互联网 发布:淘宝旺铺只能装修手机 编辑:程序博客网 时间:2024/06/15 18:18

实现功能:点击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. 实现效果如果




该效果还没有实现全屏

0 0
原创粉丝点击