延续:iOS开发基础-图片切换(3),对(3)里面的代码用懒加载进行改善。

一、懒加载基本内容

  懒加载(延迟加载):即在需要的时候才加载,修改属性的 getter 方法。

  注意:懒加载时一定要先判断该属性是否为 nil ,如果为 nil 才进行实例化。

优点:

  1) viewDidLoad 中创建对象的方法用懒加载创建,增加可读性。

  2)每个控件的 getter 方法中负责各自的实例化处理,增加代码之间的独立性。

二、代码实例

  简化 viewDidLoad 方法如下:

 - (void)viewDidLoad {
[super viewDidLoad];
[self change]; //初始化界面
}

  对2个 UILabel ,2个 UIButton 和1个 UIImageView 的 getter 方法进行修改:

 //firstLabel的getter方法
- (UILabel *)firstLabel {
//判断是否有了,若没有,则进行实例化
if (!_firstLabel) {
_firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
[_firstLabel setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:_firstLabel];
}
return _firstLabel;
} //lastLabel的getter方法
- (UILabel *)lastLabel {
if (!_lastLabel) {
_lastLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[_lastLabel setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:_lastLabel];
}
return _lastLabel;
} //imageIcon的getter方法
- (UIImageView *)imageIcon {
if (!_imageIcon) {
_imageIcon = [[UIImageView alloc] initWithFrame:CGRectMake(POTOIMAGEX, POTOIMAGEY, POTOIMAGEWIDTH, POTOIMAGEHEIGHT)];
_imageIcon.image = [UIImage imageNamed:@"beauty0"];
[self.view addSubview:_imageIcon];
}
return _imageIcon;
} //leftButton的getter方法
- (UIButton *)leftButton {
if (!_leftButton) {
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
_leftButton.frame = CGRectMake(, self.view.center.y, , );
[_leftButton setBackgroundImage:[UIImage imageNamed:@"leftRow"] forState:UIControlStateNormal];
[self.view addSubview:_leftButton];
[_leftButton addTarget:self action:@selector(leftClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _leftButton;
} //rightButton的getter方法
- (UIButton *)rightButton {
if (!_rightButton) {
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton.frame = CGRectMake(, self.view.center.y, , );
[_rightButton setBackgroundImage:[UIImage imageNamed:@"rightRow"] forState:UIControlStateNormal];
[self.view addSubview:_rightButton];
[_rightButton addTarget:self action:@selector(rightClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _rightButton;
}

参考博客:iOS开发UI篇—懒加载

实例代码:http://pan.baidu.com/s/1dElhMmP

iOS开发基础-图片切换(4)之懒加载的更多相关文章

  1. iOS开发基础-图片切换(2)之懒加载

    延续:iOS开发基础-图片切换(1),对(1)里面的代码进行改善. 在 ViewController 类中添加新的数组属性:  @property (nonatomic, strong) NSArra ...

  2. iOS开发基础-图片切换(3)之属性列表

    延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...

  3. iOS开发基础-图片切换(1)

    一.程序功能分析 1)点击左右箭头切换图片.序号.描述: 2)如果是首张图片,左边箭头失效: 3)如果是最后一张图片,右边箭头失效. 二.程序实现 定义确定图片位置.大小的常量: //ViewCont ...

  4. js不需要知道图片宽高的懒加载方法(经过实际测试,不加宽高仍然是无法正常加载的,设置height:auto,height:100%,仍然显示高度为0)

    js不需要知道图片宽高的懒加载方法 懒加载是如何实现的? - 简书https://www.jianshu.com/p/e86c61468285找到一个不需要知道图片宽高的懒加载方法了(经过实际测试,不 ...

  5. vue项目中,单页图片过多,使用懒加载

    最近做项目,一页图片很多,加载的时候效果很差. 通过学习借鉴其他大神的方法,使用了插件vue-lazyload,使用这个插件,界面更美观了,加载的效果好起来. 安装 npm i vue-lazyloa ...

  6. 【Spring注解驱动开发】使用@Lazy注解实现懒加载

    写在前面 Spring在启动时,默认会将单实例bean进行实例化,并加载到Spring容器中.也就是说,单实例bean默认在Spring容器启动的时候创建对象,并将对象加载到Spring容器中.如果我 ...

  7. 使用jquery插件实现图片延迟加载技术(懒加载)

    有时我们看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对 ...

  8. spring注解开发:bean的作用域与懒加载

    1.bean的作用域 1.新建一个maven工程,添加如下依赖 <dependency> <groupId>org.springframework</groupId> ...

  9. iOS开发UI篇—在UItableview中实现加载更多功能

    一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据.                      二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...

随机推荐

  1. 【SpringCloud】Zuul在何种情况下使用Hystrix

    首先,引入spring-cloud-starter-zuul之后会间接引入: hystrix依赖已经引入,那么何种情况下使用hystrix呢? 在Zuul的自动配置类ZuulServerAutoCon ...

  2. 2.计算机组成-数字逻辑电路 门电路与半加器 异或运算半加器 全加器组成 全加器结构 反馈电路 振荡器 存储 D T 触发器 循环移位 计数器 寄存器 传输门电路 译码器 晶体管 sram rom 微处理 计算机

    现代计算机的各个部件到底是如何通过逻辑电路构成的呢   半加器 我们说过了门电路 看似简单的三种门电路却是组成了整个逻辑电路的根基 真值表--其实就是根据输入输出状态枚举罗列出来的所有可能 比如有一台 ...

  3. 强大的jupyter,python开发者的福音

    jupyter是一种交互式计算和开发环境的笔记,ipython命令行比原生的python命令行更加友好和高效,还可以运行web版的界面,支持多语言,输出图形.音频.视频等功能. 一.安装 pip3 i ...

  4. Spring Boot 2.x(九):遇到跨域不再慌

    什么是跨域 首先,我们需要了解一下一个URL是怎么组成的: // 协议 + 域名(子域名 + 主域名) + 端口号 + 资源地址 http: + // + www.baidu.com + :8080/ ...

  5. VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——VoiceRecorder控件的使用方式.Net移动开发

    一.          样式一 我们要实现上图中的效果,需要如下的操作: 从工具栏上的“Smobiler Components”拖动一个VoiceRecorder控件和一个ImageButton控件到 ...

  6. c#调用com组件,程序 发生意外<hr=0x80020009>

    引用dll,确认dll没有问题,版本正确,可是一直报发生意外,没有任何其他提示. 解决方案: 看dll引用选项配置 复制到本地:设为true,我的就是false; 嵌入互操作类型:false,如果是t ...

  7. 【开源】SpringBoot&Netty实现仿微信网页版项目更新

    阅读本文约“2.3分钟” 项目更新啦!V1.3.0 还记得那个聊天室的小项目吗? SpringBoot 加 Netty 实现聊天室 没错,这次已经完整进行了版本的替换,酥酥聊天室! 基于原项目的改动, ...

  8. You are what you write——沈向洋

    title: You are what you write--沈向洋 date: 2018-02-21 13:18:28 tags: [随想,write] categories: 个人文章 --- A ...

  9. C++玄学预编译优化

    #pragma GCC diagnostic error "-std=c++11" #pragma GCC optimize("-fdelete-null-pointer ...

  10. MyEclipse自动补全

    打开MyEclipse 6.5,然后"window"→"Preferences". 选择"java",展开,"Editor&quo ...