Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >demo1 动态显示view或弹框 动态隐藏view或弹框

demo1 动态显示view或弹框 动态隐藏view或弹框

作者头像
用户1219438
发布于 2018-02-01 08:02:06
发布于 2018-02-01 08:02:06
1.1K00
代码可运行
举报
文章被收录于专栏:AliceAlice
运行总次数:0
代码可运行

实现界面如上所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
有一个弹框,弹框上边有一个关闭按钮,点击按钮,可以关闭弹框。点击弹框的周围区域也可以关闭按钮。 点击上边的隐藏弹框也可以关闭按钮。

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

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

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

思路如下:

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

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

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
核心代码实现:acercodeview的代码
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//

//  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的书写

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//

//  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 .

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-01-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Python 高级特性(3)- 列表生成式
日常工作中,range() 应该非常熟悉了,它可以生成一个迭代对象,然后可以使用 list() 将它转成一个 list
小菠萝测试笔记
2021/04/19
2910
python笔记21-列表生成式
python里面[]表示一个列表,快速生成一个列表可以用range()函数来生成。 对列表里面的数据进行运算和操作,生成新的列表最高效快速的办法,那就是列表生成式了。
上海-悠悠
2018/07/25
4930
python笔记21-列表生成式
盘点Python列表生成式的三种方法
列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。
Go进阶者
2021/02/07
3.7K0
python 高级特性:List Comprehensions
#common establish way lis1 = []; for x in range(1, 10):     lis1.append(x); print "lis1:", lis1;
py3study
2020/01/14
3820
python生成式
本篇将介绍Python的列表生成式,更多内容请参考:Python列表生成式 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。 举个例子,要生成list[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]可以用list(range(1, 11)) >>>list(range[1, 11]) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,] 但是要生成[1×1, 2×2, 3×3, 4×4,..., 10×1
用户1174963
2018/01/17
8050
Python-列表推导式
要生成list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]可以用list(range(0, 11)):
PayneWu
2020/12/18
5270
Python学习笔记(三)·高级特性
掌握了 Python 的数据类型、语句和函数,基本上就可以编写出很多有用的程序了。
公爵
2022/09/28
6680
Python学习笔记(三)·高级特性
Python高级特性:列表生成式
列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。
听着music睡
2018/10/25
4790
Python 列表生成式
>>> [x * xfor x in range(1, 6)]       --列表生成式
py3study
2020/01/10
7870
Python基本语法 列表生成式
举个例子,要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]可以用list(range(1, 11)):
Autooooooo
2020/11/09
3960
Python 列表生成式(List Comprehensions)
列表生成式(List Comprehensions),顾名思义,即为生成列表的表达式。列表生成式是Python内置的高级特性,简单却功能强大,充分体现了Python的简洁美。 格式: [列表形式 生成规则] #即一个列表生成式由2个表达式组成 列表生成式(List Comprehensions) 生成规则只要符合Python的语法即可,所以非常灵活,大家可以举一反三,一次类推。 下面举几个例子供大家学习,能使用到什么水平要看个人积累了。 #生成从1到10的list >>>[x for x
Steve Wang
2018/02/05
6610
python高级特性-列表生成
概述 [x *x for x in range(1,11)] [k+'='+v for k,v in d.items()] [s.lower() for s in L] 详解 1.单层迭代 >>> [x *x for x in range(1,11)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>> [x*x for x in range(1,11) if x%2 ==0] [4, 16, 36, 64, 100] 2.双层for循环 >>> [m+n for m i
yaohong
2019/09/11
5800
Python进阶笔记
列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。一般是利用原有的数据结构来生成新的列表。
py3study
2020/01/03
1.1K0
一行代码的优雅| Python列表生成式
列表是Python中非常常见的数据结构,在基础课中也占了不小的篇幅。今天的推送就列表相关的内容再整理。
生信菜鸟团
2020/06/04
3.4K0
Python列表生成式12个小功能,你常用哪几个?
我正在梳理一个系列:Python在工作中被频繁用到的那些操作,直击重点,无半句废话,欢迎学习!目前已推送:
double
2019/10/29
4810
Python学习(四)---- 列表生成式、生成器、迭代器和内置函数
https://blog.csdn.net/fgf00/article/details/52061971
智能算法
2018/08/17
1.3K0
python3_高级特性
欢迎与我分享你的看法。 转载请注明出处:http://taowusheng.cn/
yifei_
2022/11/14
1720
Python3学习笔记06——列表创建
在Python中使用for循环是,只要作用于一个可迭代对象,for循环就可以正常运行,而我们不太关心该对象究竟是list还是其他数据类型。 那么,如何判断一个对象是可迭代对象呢?方法是通过collections模块的Iterable类型判断:
py3study
2020/01/12
8700
Python学习 Day 4 函数 切片 迭代 列表生成式 生成器
TypeError: my_abs() takes exactly 1argument (2 given)#参数个数不对
统计学家
2019/04/10
3870
Python函数
阅读文本大概需要 6 分钟 写在前面 这段时间通过公号写文章结交了许多志同道合的朋友,他们中有和我一样的大学生、研究生、以及已经工作的前辈。虽然处于不同的人生阶段,但彼此聊得很 High ,每个人的成长历程中总有相似的地方,遇到的困惑迷茫也大致相同。通过相互间的交流沟通,可能困扰自己很久的问题于前辈而言只是一个小 Case ,所以说要勤于沟通,去找寻属于自己的圈子,这样你才能提升得更快。 分享给大家一个观点,提升认知优先于积累知识。我的微信个签是「努力固然重要,但请记得选择比努力更重要」因为你做出选择的前
Python技术与生活认知的分享
2018/07/03
1K0
相关推荐
Python 高级特性(3)- 列表生成式
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验