激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

服務器之家 - 編程語言 - IOS - iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

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

2021-01-11 16:53jiangamh IOS

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

以前瀑布流的時候使用過uicollectionview,但是那時使用的是系統自帶的uicollectionviewflowlayout布局,今天看文章,看到uicollectionviewflowlayout自定義相關的東西,于是動手寫了一個簡單圖片瀏覽的demo,熟練一些uicollectionviewflowlayout自定義布局。

?
1
2
3
4
5
#import <uikit/uikit.h>
 
@interface jwcollectionviewflowlayout : uicollectionviewflowlayout
 
@end

自定義uicollectionviewflowlayout,首先繼承uicollectionviewflowlayout,實現一下幾個方法

?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#define screenwidth [uiscreen mainscreen].bounds.size.width
 
#define maxchangerange 100
 
#import "jwcollectionviewflowlayout.h"
 
@implementation jwcollectionviewflowlayout
 
-(void)preparelayout
{
 self.scrolldirection = uicollectionviewscrolldirectionhorizontal;
 self.itemsize = cgsizemake(300, 500);
}
 
- (bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds
{
 return yes;
}
 
- (nullable nsarray<__kindof uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect
{
 nsarray *array = [super layoutattributesforelementsinrect:rect];
 
 cgrect visiblerect = cgrectmake(self.collectionview.contentoffset.x, 0, self.collectionview.bounds.size.width, self.collectionview.bounds.size.height);
 for (uicollectionviewlayoutattributes *attr in array)
 {
  if (cgrectintersectsrect(attr.frame, rect)) {
 
   bool isatright = yes;
   cgfloat distance = (attr.center.x - cgrectgetmidx(visiblerect));
   if (distance<0) {
    distance = -distance;
    isatright = no;
   }
   cgfloat precent ;
   if (distance < 180)
   {
    precent = 1.0;
   }
   else
   {
    precent = ((screenwidth / 2) - distance) / (screenwidth / 2);
   }
   catransform3d transform = catransform3didentity;
   transform.m34 = 1.0 / 600;
 
   if (precent < 0.5) {
    precent = 0.5;
   }
   transform = catransform3dscale(transform, 1, precent, 1);
   cgfloat p = isatright?m_pi_4:-m_pi_4;
   transform = catransform3drotate(transform, p * (1 - precent), 0, 1, 0);
   attr.transform3d = transform;
   attr.zindex = 1;
   attr.alpha = precent;
  }
 }
 
 return array;
}
 
- (cgpoint)targetcontentoffsetforproposedcontentoffset:(cgpoint)proposedcontentoffset withscrollingvelocity:(cgpoint)velocity
{
 cgfloat offset = maxfloat;
 
 cgfloat hcenter = proposedcontentoffset.x + (cgrectgetwidth(self.collectionview.bounds) / 2.0);
 
 cgrect currentrect = cgrectmake(proposedcontentoffset.x, 0, self.collectionview.bounds.size.width, self.collectionview.bounds.size.height);
 
 nsarray* array = [super layoutattributesforelementsinrect:currentrect];
 for (uicollectionviewlayoutattributes* layoutattributes in array)
 {
  cgfloat itemhorizontalcenter = layoutattributes.center.x;
  if (abs(itemhorizontalcenter - hcenter) < abs(offset))
  {
 
   offset = itemhorizontalcenter - hcenter;
  }
 }
 
 return cgpointmake(proposedcontentoffset.x + offset, proposedcontentoffset.y);
}

使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
-(void)setupui
{
 
 jwcollectionviewflowlayout *flowlayout = [[jwcollectionviewflowlayout alloc] init];
 uicollectionview *imgbrowseview = [[uicollectionview alloc] initwithframe:self.view.bounds collectionviewlayout:flowlayout];
 imgbrowseview.datasource = self;
 imgbrowseview.delegate = self;
 imgbrowseview.backgroundcolor = [uicolor whitecolor];
 [self.view addsubview:imgbrowseview];
 _imgbrowseview = imgbrowseview;
 
 [self.imgbrowseview registernib:[uinib nibwithnibname:@"custumcollectionviewcell" bundle:nil] forcellwithreuseidentifier:@"cell"];
}

demo:https://github.com/jiangtaidi/jwimagebrowsedemo.git

運行結果:

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

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

延伸 · 閱讀

精彩推薦
  • IOSiOS中MD5加密算法的介紹和使用

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

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

    LYSNote5432021-02-04
  • IOSiOS開發技巧之狀態欄字體顏色的設置方法

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

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

    夢想家-mxj8922021-05-10
  • IOSiOS開發之視圖切換

    iOS開發之視圖切換

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

    執著丶執念5272021-01-16
  • IOSiOS中UILabel實現長按復制功能實例代碼

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

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

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

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

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

    jiangamh8882021-01-11
  • IOS詳解iOS中多個網絡請求的同步問題總結

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

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

    liang199111302021-03-15
  • IOSiOS實現控制屏幕常亮不變暗的方法示例

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

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

    隨風13332021-04-02
  • IOSiOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)

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

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

    CodingFire13652021-02-26
主站蜘蛛池模板: 在线看免电影网站 | 国产91九色 | 精品中文一区 | av影院在线 | 欧美精品一级片 | 久久亚洲国产午夜精品理论片 | 美女av在线免费观看 | 久久人人人| 99精品欧美一区二区 | 久久久三区 | 夜夜看| 午夜精品久久久久久久久久久久久蜜桃 | 日韩黄色在线播放 | 国产a一级片 | 中文字幕在线免费观看电影 | 一区二区免费 | 成人在线免费观看网址 | 色女生影院 | 美国黄色毛片女人性生活片 | 日韩精品久久久久久久九岛 | 九九热久久免费视频 | av电影手机在线看 | 精品国产呦系列在线看 | 久久久久国产一区二区三区不卡 | 一级成人免费 | 亚洲第一成av人网站懂色 | 国产精品久久久久久久久久久久午夜 | 日韩av电影免费看 | 免费看成年人网站 | 91中文在线| 国产毛片视频 | 得得啪在线 | 久草在线视频福利 | 亚洲第五色综合网 | 视频www| 精品国产一区二区三区久久久狼牙 | 九七在线视频 | 国产精选电影免费在线观看 | 欧产日产国产精品乱噜噜 | 99国内精品视频 | 亚洲精品一区二区三区大胸 |