实现界面如上所示:

有一个弹框,弹框上边有一个关闭按钮,点击按钮,可以关闭弹框。点击弹框的周围区域也可以关闭按钮。 点击上边的隐藏弹框也可以关闭按钮。

在实现功能的基础上,以动画的形式展示跟隐藏。

思路:在之前的开发中,我的思路比较局限。想着用一个view来做中间的那一块,那么问题来了,左上角的关闭按钮,就加在view的左上角。效果猛一看是可以实现,但是这个关闭按钮的点击事件,却不怎么好使,因为按钮有一部分超出了view的界限,于是,点击起来就不太好使。

遇见问题,解决问题。于是我就转换了一种思路。当然这思路还是在别人的指点下完成的。

思路如下:

1.首先确实需要一个弹框的view1 view1的大小是整个界面的大小。设置这个view的背景为半透明,透明度可以是0.5 或者是任意0-1之间的数值,具体看你想要的效果。

2.然后需要一个放内容的view2 这个view2里边包含了 上边的img 还有两行文字,都是放在这个view2里边的。

3.最后将关闭按钮 加在view1的上边。这样就大功告成了。 随便点击关闭按钮,丝毫没有任何印象。

核心代码实现:
acercodeview的代码
//

//  ACErCodeView.m

//  demo1二维码点击动态出现

//

//  Created by Alice_ss on 2018/1/3.

//  Copyright © 2018年 AC. All rights reserved.

//

#import "ACErCodeView.h"

#define SCREENW [UIScreen mainScreen].bounds.size.width

#define SCREENH [UIScreen mainScreen].bounds.size.height

@implementation ACErCodeView{

    UIImageView *codeIMG;

    UILabel *nickNameLabel;

    UILabel *sexLabel;

    UIButton *closeBtn;

}

-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {

        [self createUI];

    }

    return  self;

}

- (void)createUI{

    //1.创建一个view背景设置呈透明的因为这样的话才能将关闭按钮悬浮在上边。

    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, SCREENW,SCREENH)];

    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClose)];

    bgView.userInteractionEnabled = YES;

    [bgView addGestureRecognizer:tap];

    [self addSubview:bgView];

    //2.放内容的大view

    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW-120,SCREENH-200)];

    contentView.backgroundColor = [UIColor whiteColor];

    [self addSubview:contentView];

    //3.二维码图片

    codeIMG = [[UIImageView alloc]initWithFrame:CGRectMake((CGRectGetWidth(contentView.frame)-100)/2, CGRectGetMinY(contentView.frame), 100, 100)];

    codeIMG.backgroundColor = [UIColor redColor];

    [contentView addSubview:codeIMG];

    //4.昵称

    nickNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(codeIMG.frame)+10, CGRectGetWidth(contentView.frame), 44)];

//    nickNameLabel.backgroundColor = [UIColor blueColor];

    nickNameLabel.text = @"我是你们喜欢的Alice";

    nickNameLabel.textAlignment = NSTextAlignmentCenter;

    [contentView addSubview:nickNameLabel];

    //5.sex

    sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(nickNameLabel.frame)+10, CGRectGetWidth(contentView.frame), 44)];

//    sexLabel.backgroundColor = [UIColor redColor];

    sexLabel.text= @"我的性别是一个漂亮的小美女哦";

    sexLabel.textAlignment  = NSTextAlignmentCenter;

    [contentView addSubview:sexLabel];

    //给contentview设置高度

    contentView.frame = CGRectMake(SCREENW/2-(SCREENW-120)/2, (SCREENH-(CGRectGetMaxY(sexLabel.frame)+10))/2, SCREENW-120, CGRectGetMaxY(sexLabel.frame)+10);

    //6.关闭按钮

    closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMinX(contentView.frame)-10, CGRectGetMinY(contentView.frame)-10, 30, 30)

                ];

    [closeBtn setBackgroundImage:[UIImage imageNamed:@"icon_shouye_GuanBi.png"] forState:0];

    [closeBtn addTarget:self action:@selector(closeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:closeBtn];

}

//关闭页面

- (void)closeBtnClicked:(UIButton*)sender{

    [UIView animateWithDuration:0.3 animations:^{

        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.3, 0.3);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.2 animations:^{

            self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0, 0);

        }];

        self.hidden = YES;

        self.blockCloseClicked(self.hidden);

    }];

}

//点击背景隐藏界面

- (void)tapClose{

    [UIView animateWithDuration:0.3 animations:^{

        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.3, 0.3);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.2 animations:^{

            self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0, 0);

        }];

        self.hidden = YES;

        self.blockCloseClicked(self.hidden);

    }];

}

@end

//viewcontroller的书写

//

//  Created by Alice_ss on 2018/1/3.

//  Copyright © 2018年 AC. All rights reserved.

//

#import "ViewController.h"

#import "ACErCodeView.h"

#define SCREENW [UIScreen mainScreen].bounds.size.width

#define SCREENH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIBarButtonItem *navRBarItem;

@property(nonatomic,strong)ACErCodeView *erCodeIMG;

@end

@implementation ViewController

- (IBAction)naviRBarClicked:(UIBarButtonItem *)sender {

    NSLog(@"RIGHT BAR ITEM CLICKED");

    if(_erCodeIMG.hidden){

        _erCodeIMG.hidden = NO;

        sender.title = @"点击隐藏";

        _erCodeIMG.transform = CGAffineTransformScale(CGAffineTransformIdentity, CGFLOAT_MIN, CGFLOAT_MIN);

        [UIView animateWithDuration:0.3 animations:^{

            self.erCodeIMG.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.2 animations:^{

                _erCodeIMG.transform = CGAffineTransformIdentity;

            }];

        }];

    }else{

        sender.title = @"点击显示";

        [UIView animateWithDuration:0.3 animations:^{

            self.erCodeIMG.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.3, 0.3);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.2 animations:^{

                _erCodeIMG.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0, 0);

            }];

            _erCodeIMG.hidden = YES;

        }];

    }

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    // Do any additional setup after loading the view, typically from a nib.

    _erCodeIMG = [[ACErCodeView alloc]initWithFrame:CGRectMake(0, 0,SCREENW , SCREENH)];

    _erCodeIMG.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];

    _erCodeIMG.hidden = YES;

    __weak typeof(self) weakSelf = self;

    _erCodeIMG.blockCloseClicked = ^(BOOL ishidden) {

        if (ishidden) {

            self.navRBarItem.title = @"点击显示";

        }else{

            self.navRBarItem.title = @"点击隐藏";

        }

    };

    [self.view addSubview:_erCodeIMG];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

以上就是全部代码。

希望新的一年,自己工作越来越踏实。同时也要学会拒绝,学会给与。

如有任何问题。请联系我的邮箱 673658917@qq.com .

demo1 动态显示view或弹框 动态隐藏view或弹框的更多相关文章

  1. Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解

    废话不多说,本文将会层层深入给大家讲解如何动态的生成一个完整的界面. 本文内容: Java代码中动态生成View Java代码中动态设置View的位置,以及其他的属性 LayoutParams详解 一 ...

  2. CCOMBOX下拉弹出框,因属性对话框自动隐藏而弹出框没有隐藏问题

    关于这个问题是可以使用 使其失去焦点 releasecapture()解决的,但是鼠标在下拉列表中的item中经过时,调用releasecapture()后会选中最后mousemove过的item项. ...

  3. bootstrap模态框动态赋值, ajax异步请求数据后给id为queryInfo的模态框赋值并弹出模态框(JS)

    /查询单个 function query(id) { $.ajax({ url : "/small/productServlet", async : true, type : &q ...

  4. 键盘弹出后上提view隐藏后下拉view还原并修改scroll过程中旋转屏幕到竖屏view显示错误

    1,注册键盘相应事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillSho ...

  5. go语言使用go-sciter创建桌面应用(七) view对象常用方法,文件选择,窗口弹出,请求

    view对象的详细文档请看: https://sciter.com/docs/content/sciter/View.htm demo9.html代码如下: <!DOCTYPE html> ...

  6. Android 判断软键盘弹出并隐藏的简单完美解决方案

    最近项目中有一个编辑框,下面是个ListView.在触发编辑框弹出软键盘后,ListView还能滑动,并且ListView的item还能响应单击.这样的体验效果很不好.于是便想在滑动或单击item时判 ...

  7. 手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法

    手机浏览器浏览WebApp弹出的键盘遮盖住文本框的解决办法 最近碰到Android微信内置浏览H5页面,因为其中的文本输入框(input)放置在靠近页面的中下方,点击文本框以后,则输入框会被弹出的手机 ...

  8. 第三十九篇、NavBar动态隐藏、设置透明、毛玻璃效果

    1.动态隐藏 - (void)viewDidLoad { [super viewDidLoad]; if ([self respondsToSelector:@selector(automatical ...

  9. 有几数组表单,js怎么获得数组并动态相加输出到文本框

    有几数组表单,js如何获得数组并动态相加输出到文本框<input   name= "fee1[] "> <input   name= "fee2[] & ...

随机推荐

  1. SQLserver-MySQL的区别和用法

    对于程序开发人员而言,目前使用最流行的两种后台数据库即为MySQL and SQL Server.这两者最基本的相似之处在于数据存储和属于查询系统.你可以使用SQL来访问这两种数据库的数据,因为它们都 ...

  2. Tomcat双击startup.bat闪退的原因及解决方式

    很久不碰Tomcat了,最近因为种种原因需要重新投入到Java Web的怀抱,所以又重新接触了Tomcat 我下载了tomcat的压缩包将其解压缩到某个位置,我这里是D盘下的tomcat文件夹中,但是 ...

  3. 数据可视化之DAX篇(十)在PowerBI中累计求和的两种方式

    https://zhuanlan.zhihu.com/p/64418286 假设有一组数据, 已知每一个产品贡献的利润,如果要计算前几名产品的贡献利润总和,或者每一个产品和利润更高产品的累计贡献占总体 ...

  4. 03-Python控制语句

    一.简介 通过一些语句来改变程序的执行顺序,这些语句被叫做控制语句,在python主要有if.for.while三种控制流语句. 二.if语句 用来检测一个条件是否成立,如果为真,则执行该语句(一般为 ...

  5. Python3 迭代器深入解析

    第6章 函数 6.1 函数的定义和调用 6.2 参数传递 6.3 函数返回值 6.4 变量作用域 6.5 匿名函数(lambda) 6.6 递归函数 6.7 迭代器 6.8 生成器 6.9 装饰器 6 ...

  6. ModuleNotFoundError: No module named 'phkit.pinyin'

    1 产生背景 在mac系统本地使用正常,在linux系统上phkit包缺少相应的python文件 2 解决方案 自己想出来,手动上传本地相关python代码到linux服务器 3 解决过程 首先通过项 ...

  7. mybatis generator 的日常使用

    一.mybatis-generator的基本配置与使用 使用mybatis-generator来生成常用的dao层类与xml,可以满足基础的增删改查功能 1. 添加pom依赖,常用的几个依赖包 < ...

  8. 高阶Pandas知识图谱-《利用Python进行数据分析》

    所有内容整理自<利用Python进行数据分析>,使用MindMaster Pro 7.3制作,emmx格式,源文件已经上传Github,需要的同学转左上角自行下载或者右击保存图片. 其他章 ...

  9. 05 . ELK Stack+Redis日志收集平台

    环境清单 IP hostname 软件 配置要求 网络 备注 192.168.43.176 ES/数据存储 elasticsearch-7.2 内存2GB/硬盘40GB Nat,内网 192.168. ...

  10. 云原生时代高性能Java框架—Quarkus(二)

    --- *构建Quarkus本地镜像.容器化部署Quarkus项目* Quarkus系列博文 Quarkus&GraalVM介绍.创建并启动第一个项目 构建Quarkus本地镜像.容器化部署Q ...