国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - IOS - IOS封裝自定義布局的方法

IOS封裝自定義布局的方法

2021-01-04 18:09世俗孤島 IOS

這篇文章主要介紹了IOS封裝自定義布局的方法,需要的朋友可以參考下

一、概述
1、對于經常使用的控件或類,通常將其分裝為一個單獨的類來供外界使用,以此達到事半功倍的效果
2、由于分裝的類不依賴于其他的類,所以若要使用該類,可直接將該類拖進項目文件即可
3、在進行分裝的時候,通常需要用到代理設計模式
二、代理設計模式
1、代理設計模式的組成
客戶類(通常作為代理):通常委托這是角色來完成業務邏輯
真實角色:將客戶類的業務邏輯轉化為方法列表,即代理協議
代理協議:

  • 定義了需要實現的業務邏輯
  • 定義了一組方法列表,包括必須實現的方法或選擇實現的方法
  • 代理協議是代理對象所要遵循一組規則

代理角色

  • 若要作為代理,需要遵守代理協議,并且實現必須實現的代理方法
  • 代理角色可以通過調用代理協議中的方法完成業務邏輯,也可以附加自己的操作

文字描述通常是抽象的,一下通過圖示來闡述代理設計模式

IOS封裝自定義布局的方法

三、自定義布局類的封裝
1、業務邏輯
如圖

IOS封裝自定義布局的方法

2、布局每個cell的業務邏輯
由于設置每個cell的布局屬性的業務邏輯較復雜,特附上如下思維導圖

IOS封裝自定義布局的方法

3、封裝思路封裝需要根據客戶類業務邏輯需求來提供接口
1)、通過代理協議的可選實現的方法獲取的屬性值的屬性,需要設置默認值
2)、未提供默認值的且必須使用的屬性,需要通過必須實現的方法來獲得
3)、自定義布局提供的接口可選

  • 列數
  • 列之間的間距
  • 行之間的間距
  • 內邊距

4)、自定義布局提供的接口必選
每個元素的高度,寬度可以通過列數和列間距計算得到
四、封裝步驟
設置代理協議,提供接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//聲明lypwaterflowlayout為一個類
@class lypwaterflowlayout;
@protocol lypwaterflowlayoutdelegate <nsobject>
//必須實現的方法
@required
/**獲取瀑布流每個元素的高度*/
- (cgfloat)waterflowlayout:(lypwaterflowlayout *)waterflowlayout heightforitematindex:(nsinteger)index itemwith:(cgfloat)itemwith;
//可選實現的方法
@optional
/**獲取瀑布流的列數*/
- (nsinteger)columncountinwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
/**獲取瀑布流列間距*/
- (cgfloat)columnmargininwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
/**獲取瀑布流的行間距*/
- (cgfloat)rowmargininwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
/**獲取瀑布流的內邊距*/
- (uiedgeinsets)edgeinsetsinwaterflowlayout:(lypwaterflowlayout *)waterflowlayout;
@end

設置代理屬性

?
1
2
3
4
@interface lypwaterflowlayout : uicollectionviewlayout
/**代理*/
@property (nonatomic, weak) id<lypwaterflowlayoutdelegate> delegate;
@end

設置通過可選代理方法獲取屬性值的屬性的默認值

?
1
2
3
4
5
6
7
8
/**默認的列數*/
static const nsinteger lypdefaultcolumncount = 3;
/**默認每一列之間的間距*/
static const cgfloat lypdefaultcolummargin = 10;
/**默認每一行之間的間距*/
static const cgfloat lypdefaultrowmargin = 10;
/**默認邊緣間距*/
static const uiedgeinsets lypdefaultedgeinsets = {10, 10, 10, 10};

設置通過可選代理方法獲取屬性值的屬性的訪問方式若代理提供屬性值,則忽略默認值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (nsinteger)columncount
{
  //判斷代理是否實現了獲取列數的可選方法
  if ([self.delegate respondstoselector:@selector(columncountinwaterflowlayout:)])
  {
    //實現,返回通過代理設置的列數
    return [self.delegate columncountinwaterflowlayout:self];
  }
  else
  {
    //為實現,返回默認的列數
    return lypdefaultcolumncount;
  }
}

注:其他屬性值的獲取與上述方法幾乎完全相同,不再贅述
設置布局
1)、設置需要的成員屬性

?
1
2
3
4
/**所有cell的布局屬性*/
@property (nonatomic, strong) nsmutablearray *attrsarray;
/**所有列的當前高度*/
@property (nonatomic, strong) nsmutablearray *columnheights;

2)、通過懶加載的方式初始化成員屬性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**--attrsarray--懶加載*/
- (nsmutablearray *)attrsarray
{
  if (_attrsarray == nil)
  {
    _attrsarray = [nsmutablearray array];
  }
  return _attrsarray;
}
/**--columnheights--懶加載*/
- (nsmutablearray *)columnheights
{
  if (_columnheights == nil)
  {
    _columnheights = [nsmutablearray array];
  }
  return _columnheights;
}

3)、初始化布局

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (void)preparelayout
{
  [super preparelayout];
 
  /**清除之前跟布局相關的所有屬性,重新設置新的布局*/
  //清除之前計算的所有列的高度
  [self.columnheights removeallobjects];
  //設置所有列的初始高度
  for (nsinteger i = 0; i<self.columncount; i++)
  {
    self.columnheights[i] = @(self.edgeinsets.top);
  }
  //清除之前所有的布局屬性
  [self.attrsarray removeallobjects];
 
  /**開始創建每一個cell對應的布局屬性*/
  nsinteger count = [self.collectionview numberofitemsinsection:0];
  for (nsinteger i = 0; i<count; i++)
  {
    nsindexpath *indexpath = [nsindexpath indexpathforitem:i insection:0];
    //獲取indexpath位置cell對應的布局屬性
    uicollectionviewlayoutattributes *attrs = [self layoutattributesforitematindexpath:indexpath];
    //將indexpath位置的cell的布局屬性添加到所有cell的布局屬性數組中
    [self.attrsarray addobject:attrs];
  }
}

4)、返回包含所有cell的布局屬性的數組

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
- (nullable nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect
{
  return self.attrsarray;
}
設置每一個cell的布局屬性
 
- (nullable uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nonnull nsindexpath *)indexpath
{
  //獲取indexpath位置的布局屬性
  uicollectionviewlayoutattributes *attrs = [uicollectionviewlayoutattributes layoutattributesforcellwithindexpath:indexpath];
 
  /**設置cell布局屬性的frame*/
 
  /***確定cell的尺寸***/
  //獲取collectionview的寬度
  cgfloat collectionviewwidth = self.collectionview.frame.size.width;
  //cell寬度
  cgfloat width = ((collectionviewwidth - self.edgeinsets.left - self.edgeinsets.right - (self.columncount - 1) * self.colummargin)) / self.columncount;
  //cell高度
  cgfloat height = [self.delegate waterflowlayout:self heightforitematindex:indexpath.item itemwith:width];
 
  /***設置cell的位置***/
  nsinteger destcolumn = 0;
  cgfloat mincolumnheight = [self.columnheights[0] doublevalue];
  for (nsinteger i = 1; i<self.columncount; i++)
  {
    cgfloat columnheight = [self.columnheights[i] doublevalue];
    if (mincolumnheight > columnheight)
    {
      mincolumnheight = columnheight;
      destcolumn = i;
    }
  }
  //計算cell的位置
  cgfloat x = self.edgeinsets.left + destcolumn * (width + self.colummargin);
  cgfloat y = mincolumnheight;
  //判斷是不是第一行
  if (y != self.edgeinsets.top)
  {
    //若不是第一行,需要加上行間距
    y += self.rowmargin;
  }
 
  /**給cell的布局屬性的frame賦值*/
  attrs.frame = cgrectmake(x, y, width, height);
 
  //更新最短那列的高度
  self.columnheights[destcolumn] = @(cgrectgetmaxy(attrs.frame));
 
  /**返回indexpath位置的cell的布局屬性*/
  return attrs;
}

5)、設置collectionview內容的尺寸

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (cgsize)collectionviewcontentsize
{
  //獲取最高的那一列的高度
  cgfloat maxcolumnheight = [self.columnheights[0] doublevalue];
  for (nsinteger i = 1; i<self.columncount; i++)
  {
    cgfloat columnheight = [self.columnheights[i] doublevalue];
    if (maxcolumnheight < columnheight)
    {
      maxcolumnheight = columnheight;
    }
  }
  //返回collectionview的contentsize,高度為最高的高度加上一個行間距
  return cgsizemake(0, maxcolumnheight + self.rowmargin);
}

以上就是本文的全部內容,希望對大家的學習有所幫助。

延伸 · 閱讀

精彩推薦
  • IOSiOS實現控制屏幕常亮不變暗的方法示例

    iOS實現控制屏幕常亮不變暗的方法示例

    最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關于利用iOS如何實現控制屏幕常亮不變暗的方法,文中給出了詳細的...

    隨風13332021-04-02
  • IOS詳解iOS中多個網絡請求的同步問題總結

    詳解iOS中多個網絡請求的同步問題總結

    這篇文章主要介紹了詳解iOS中多個網絡請求的同步問題總結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    liang199111302021-03-15
  • IOSiOS開發之視圖切換

    iOS開發之視圖切換

    在iOS開發中視圖的切換是很頻繁的,獨立的視圖應用在實際開發過程中并不常見,除非你的應用足夠簡單。在iOS開發中常用的視圖切換有三種,今天我們將...

    執著丶執念5272021-01-16
  • IOSiOS中MD5加密算法的介紹和使用

    iOS中MD5加密算法的介紹和使用

    MD5加密是最常用的加密方法之一,是從一段字符串中通過相應特征生成一段32位的數字字母混合碼。對輸入信息生成唯一的128位散列值(32個字符)。這篇文...

    LYSNote5432021-02-04
  • IOSiOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果的相關資料,需要的朋友可以參考下...

    jiangamh8882021-01-11
  • IOSiOS開發技巧之狀態欄字體顏色的設置方法

    iOS開發技巧之狀態欄字體顏色的設置方法

    有時候我們需要根據不同的背景修改狀態欄字體的顏色,下面這篇文章主要給大家介紹了關于iOS開發技巧之狀態欄字體顏色的設置方法,文中通過示例代碼...

    夢想家-mxj8922021-05-10
  • IOSiOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)

    iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和

    這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關資料,需要的朋友可以參考下...

    CodingFire13652021-02-26
  • IOSiOS中UILabel實現長按復制功能實例代碼

    iOS中UILabel實現長按復制功能實例代碼

    在iOS開發過程中,有時候會用到UILabel展示的內容,那么就設計到點擊UILabel復制它上面展示的內容的功能,也就是Label長按復制功能,下面這篇文章主要給大...

    devilx12792021-04-02
主站蜘蛛池模板: 国产中文字幕在线观看 | 国产精品久久久久久久久久 | 久久免费电影 | 国产一区二区视频精品 | 日本久久影视 | 精品三级 | 玖玖色资源 | 久久涩| 国产成人高清在线 | 黄色片com| 中文字幕一区在线 | 一区二区福利 | 国产日韩精品在线 | 日韩小视频 | 日韩美女乱淫aaa高清视频 | 四季久久免费一区二区三区四区 | 久久久久久久久久久精 | 欧美一级免费 | 婷婷久久五月 | 精品在线一区二区三区 | 国产有码 | 伊人精品视频 | 久久精品小视频 | 中文字幕91在线 | 日韩欧美国产精品 | 国产999精品久久久影片官网 | 精品成人av| 亚洲精品二区三区 | 久草视频网 | 黄色网页免费看 | 欧美综合激情 | 精品国产一区二区在线 | 欧美日韩一区二区三区在线观看 | 国产一区二区欧美 | 精品在线一区二区 | 亚洲国产成人精品女人久久久 | 欧美精品一区二 | 国产在线精品一区二区三区 | 亚洲精品一区在线观看 | 欧美成人观看 | 精品国产一区二区三区性色av |