zoukankan      html  css  js  c++  java
  • IOS开发-懒加载延迟加载-图片浏览器实例

    一. 概述

    主要介绍IOS开发中的懒加载,之后在实例中应用这种懒加载方式。实例是一个使用UIImageView、UILabel、UIButton三个控件实现的图片浏览器。
     
    二. 属性的懒加载
    1)何为懒加载:
    懒加载也称为懒汉模式或延迟加载,这种方式是指让程序推迟访问数据,只有用到数据时才会创建并加载数据。
    2)懒加载的好处:
    这样做可以保证不必在程序启动时就加载数据,因为有时候有些数据不是需要用的,只是在某些操作下才会使用数据,而每次访问数据都是比较耗时且占用一定的内存,使用懒加载就可以避免这中情况的发生。
    如何实现懒加载:
    一般的做法是重写某个属性的get方法,在get方法中判空即判断数据是否已经加载如果没有加载则创建数据并初始化否则直接返回数据。具体的做法见下文实例。
    三. 懒加载使用实例图片浏览器
    界面如图:
                                   
    》》》功能描述:
    1. 点击左右箭头切换图片、图片描述、图片序号;
    2.第一张图片时左箭头不能点击
    3.最后一张图片时右箭头不能点击
    4.点击设置按钮出现一个可设置的界面(上图中黄色背景的部分)可以设置模式和对图片的缩放
    》》》实现思路
    1. 搭建UI界面,由于只需要显现图片不需要处理点击事件故使用UIImageView控件来显示图片。
    2. 监听个按钮的点击,本程序共有前、后、设置、模式选择开关、图片缩放等点击事件所以添加事件的处理方法
    3. 前、后按钮的处理:由于点击这两个按钮要同时改变图片的内容、描述及序号,而内容、描述这两种数据我们可看成一个组合,存放入字典中,最后将每个组合存入数组而序号作为他们的索引,把初始的索引置为0,点击▶️时索引加1,反之索引减一;
    4. 模式选择处理:通常夜间模式会使屏幕变暗,再此只需将控制器的View背景颜色置为黑色
    5.图片的缩放处理,只需改变UIImageView的形变属性即可
    6. 对所有的图片处理使用懒加载的方式。
    》》》实现代码如下:
    分为几个部分:
    1.数据初始化:使用懒加载的方式,将所有的图片数据存入数组,使控制器拥有数组这个属性,并重写其get方法,在get方法中创建并加载数据
    1)控制器属性及方法,
     1 #import "ViewController.h"
     2 
     3 //用于查找字典防止拼写错误
     4 #define HGImgKey @"name"
     5 #define HGImgDes @"desc"
     6 
     7 @interface ViewController ()
     8 /** 图片序号 */
     9 @property (weak, nonatomic) IBOutlet UILabel *iconNum;
    10 /** 图片描述 */
    11 @property (weak, nonatomic) IBOutlet UILabel *iconDesc;
    12 /** 图片数据 */
    13 @property (weak, nonatomic) IBOutlet UIImageView *iconView;
    14 /** 上一张图片按钮 用于设置enable状态 */
    15 @property (weak, nonatomic) IBOutlet UIButton *prevBtn;
    16 /** 下一张图片按钮 用于设置enable状态 */
    17 @property (weak, nonatomic) IBOutlet UIButton *nextBtn;
    18 /** 设置按钮 弹出的设置框 */
    19 @property (weak, nonatomic) IBOutlet UIView *setView;
    20 
    21 /** 记录当前图片索引即序号 */
    22 @property (nonatomic,assign) int iconIndex;
    23 
    24 /** 图片数据结合 让控制器拥有数据并使用懒加载方式加载数据 */
    25 @property (nonatomic,strong)NSArray* imageData;
    26 /** 设置按钮监听事件 */
    27 - (IBAction)setting:(UIButton *)sender;
    28 /** 缩放监听事件 */
    29 - (IBAction)changeSize:(UISlider *)sender;
    30 /** 模式选择开关监听事件 */
    31 - (IBAction)changeMode:(UISwitch *)sender;
    32 
    33 /** previous 按钮监听事件 */
    34 - (IBAction)previous:(UIButton *)sender;
    35 /** next 按钮监听事件 */
    36 - (IBAction)next:(UIButton *)sender;
    37 
    38 @end
    View Code

    2)懒加载的方式创建并初始化数据

     1 /**
     2  *    懒加载的实现方式,重写get方法,用到时才创建并初始化数据
     3  *
     4  *    @return _imageData 完成初始化的数据
     5  */
     6 - (NSArray *)imageData1
     7 {
     8     // 判空只需在第一次使用时加载
     9     if(_imageData == nil)
    10     {
    11         //创建字典
    12         // 先来一个空的
    13         NSMutableDictionary *img1 = [NSMutableDictionary dictionary];
    14         // 之后使用添加的方法添加key和val
    15         img1[HGImgKey] = @"iq";
    16         img1[HGImgDes] = @"你是在挑战小偷的智商吗";
    17         
    18         // 先来一个空的
    19         NSMutableDictionary *img2 = [NSMutableDictionary dictionary];
    20         // 之后使用添加的方法添加key和val
    21         img2[HGImgKey] = @"caifang";
    22         img2[HGImgDes] = @"哪家电视台这么坑爹";
    23         
    24         // 先来一个空的
    25         NSMutableDictionary *img3 = [NSMutableDictionary dictionary];
    26         // 之后使用添加的方法添加key和val
    27         img3[HGImgKey] = @"niupai";
    28         img3[HGImgDes] = @"吃个牛排堪比杀牛";
    29         
    30         // 先来一个空的
    31         NSMutableDictionary *img4 = [NSMutableDictionary dictionary];
    32         // 之后使用添加的方法添加key和val
    33         img4[HGImgKey] = @"biaoqingdi";
    34         img4[HGImgDes] = @"在他面前,什么表情都弱爆了";
    35         
    36         // 先来一个空的
    37         NSMutableDictionary *img5 = [NSMutableDictionary dictionary];
    38         // 之后使用添加的方法添加key和val
    39         img5[HGImgKey] = @"can";
    40         img5[HGImgDes] = @"一个字惨!";
    41         
    42         // 先来一个空的
    43         NSMutableDictionary *img6 = [NSMutableDictionary dictionary];
    44         // 之后使用添加的方法添加key和val
    45         img6[HGImgKey] = @"sb";
    46         img6[HGImgDes] = @"非要跟自己过不去";
    47         
    48         /* 将字典加入数组 */
    49         // 快速创建数组的方法
    50         _imageData = @[img1,img2,img3,img4,img5,img6];
    51     
    52     }
    53     return _imageData;
    54 }
    View Code

    2.前、后按钮的监听事件:由于两个按钮均需要改变显示数据,故将公共的代码部分提出到changeData方法中,只在各自的处理中操作index即可

     1 - (IBAction)previous:(UIButton *)sender
     2 {
     3     // 序号减一
     4     _iconIndex--;
     5     // 改变显示的数据并设置按钮状态
     6     [self changeData];
     7 }
     8 
     9 - (IBAction)next:(UIButton *)sender
    10 {
    11     // 序号加1
    12     _iconIndex++;
    13     [self changeData];
    14 }
    View Code

    changeData方法实现:

     1 - (void)changeData
     2 {
     3     // 从字典中取出当前索引对应的数据
     4     NSDictionary *dict = self.imageData[self.iconIndex];
     5     
     6     /* 设置序号 */
     7     NSString *num = [NSString stringWithFormat:@"%d/%d",_iconIndex + 1,(int)self.imageData.count];
     8     _iconNum.text = num;
     9     
    10     /* 设置数据 */
    11     _iconView.image = [UIImage imageNamed:dict[HGImgKey]];
    12     
    13     /* 设置描述 */
    14     _iconDesc.text = dict[HGImgDes];
    15     
    16     // 设置按钮状态
    17     _prevBtn.enabled = _iconIndex > 0;
    18     // 不可写死
    19     //_nextBtn.enabled = _iconIndex < 5;
    20     _nextBtn.enabled = _iconIndex < self.imageData.count -1;
    21 }
    View Code

    3.设置按钮的监听事件

     1 - (IBAction)setting:(UIButton *)sender {
     2     
     3     NSLog(@"seting");
     4     
     5     // 添加动画
     6     [UIView animateWithDuration:1.0 animations:^{
     7         CGRect fram =  _setView.frame;
     8         if (fram.origin.y == self.view.frame.size.height)
     9         {
    10             fram.origin.y -= _setView.bounds.size.height;
    11         }
    12         else
    13         {
    14             fram.origin.y += _setView.bounds.size.height;
    15         }
    16         _setView.frame = fram;
    17     }];
    18 }
    View Code

    4.图片的缩放:缩放使用的是UISlider控件值设为0-1默认为最大,只能比初始状态小。

    1 - (IBAction)changeSize:(UISlider *)sender {
    2     
    3      NSLog(@"changeSize");
    4     // 缩放图片
    5     // 获取当前的value
    6     CGFloat value  = sender.value;
    7     //改变图片的transform属性
    8      _iconView.transform = CGAffineTransformMakeScale(value, value);
    9 }
    View Code 
    补充:
    1. UIlabel控件显示文字的换行,默认情况下UILabel只能显示一行如果文字过多会以“...”代替,为了避免这种情况在main.storybord中如下设置:
    将lines属性设置为0;对应的代码为numberOfLines属性
    1 @property(nonatomic) NSInteger numberOfLines;

    1. UIlabel控件显示文字默认是左对齐,要想设置的居中,在storybord中的设置见上图中alignment选项,对应的代码为:

    @property(nonatomic)        NSTextAlignment    textAlignment;   // default is NSTextAlignmentLeft

    可设置为如下三个值:

     NSTextAlignmentLeft      = 0,    // 左对齐
     NSTextAlignmentCenter    = 1,    // 居中
     NSTextAlignmentRight     = 2,    // 右对齐
     
     
     
  • 相关阅读:
    PAT (Advanced Level) Practice 1071 Speech Patterns (25分)
    PAT (Advanced Level) Practice 1070 Mooncake (25分)
    PAT (Advanced Level) Practice 1069 The Black Hole of Numbers (20分)
    PAT (Advanced Level) Practice 1074 Reversing Linked List (25分)
    PAT (Advanced Level) Practice 1073 Scientific Notation (20分)
    第一次冲刺个人总结01
    构建之法阅读笔记01
    人月神话阅读笔记01
    四则运算2
    学习进度条(软件工程概论1-8周)
  • 原文地址:https://www.cnblogs.com/jianghg/p/4479597.html
Copyright © 2011-2022 走看看