- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章ios uicollectionview实现横向滚动由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo 实现上我选择了使用uicollectionview ;用uicollectionviewflowlayout来定制样式;下面看看具体实现 。
效果 。
实现上我选择了使用uicollectionview ;用uicollectionviewflowlayout来定制样式;下面看看具体实现 。
具体实现 。
1、viviewcontroller.m 代码实现 。
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
#import "viewcontroller.h"
#import "collmodel.h"
#define screen_width [uiscreen mainscreen].bounds.size.width
#define screen_height [uiscreen mainscreen].bounds.size.height
#define screen_rate ([uiscreen mainscreen].bounds.size.width/375.0)
#import "imagecell.h"
#import "lhhorizontalpageflowlayout.h"
static
nsstring *
const
imagec = @
"imagecell"
;
static
nsstring *
const
moreimagec = @
"imagecell"
;
static
const
nsinteger kitemcountperrow = 5;
//每行显示5个
static
const
nsinteger krowcount = 3;
//每页显示行数
static
float
imageheight = 80;
//cell 高度
@interface viewcontroller ()<uicollectionviewdelegate,uicollectionviewdatasource>
@property (nonatomic, strong) uicollectionview * collectionview;
@property (nonatomic, strong) nsmutablearray * modelarray;
@property (nonatomic, strong) uicollectionview * morecollectionview;
@end
@implementation viewcontroller
- (
void
)viewdidload {
[super viewdidload];
nsarray *apparray = [[self getdict] objectforkey:@
"dictinfo"
];
for
(
int
i = 0; i < apparray.count; i++) {
nsdictionary * appdic = apparray[i];
collmodel * model = [[collmodel alloc]init];
model.title = [appdic objectforkey:@
"title"
];
model.url = [appdic objectforkey:@
"url"
];
[self.modelarray addobject:model];
}
[self createcollectionview];
[self createrightcollectionview];
}
- (nsdictionary *)getdict {
nsstring * string = @
"{\"dictinfo\":[{\"title\":\"你好啊\",\"url\":\"1.jpeg\"},{\"title\":\"你好啊\",\"url\":\"2.jpeg\"},{\"title\":\"你好啊\",\"url\":\"3.jpeg\"},{\"title\":\"你好啊\",\"url\":\"4.jpeg\"},{\"title\":\"你好啊\",\"url\":\"5.jpeg\"},{\"title\":\"你好啊\",\"url\":\"6.jpeg\"},{\"title\":\"是很好\",\"url\":\"7.jpeg\"},{\"title\":\"你好啊\",\"url\":\"1.jpeg\"},{\"title\":\"你好啊\",\"url\":\"2.jpeg\"},{\"title\":\"你好啊\",\"url\":\"3.jpeg\"},{\"title\":\"你好啊\",\"url\":\"4.jpeg\"},{\"title\":\"你好啊\",\"url\":\"5.jpeg\"},{\"title\":\"你好啊\",\"url\":\"6.jpeg\"},{\"title\":\"是很好\",\"url\":\"7.jpeg\"},{\"title\":\"你好啊\",\"url\":\"1.jpeg\"},{\"title\":\"你好啊\",\"url\":\"2.jpeg\"},{\"title\":\"你好啊\",\"url\":\"3.jpeg\"},{\"title\":\"你好啊\",\"url\":\"4.jpeg\"},{\"title\":\"你好啊\",\"url\":\"5.jpeg\"},{\"title\":\"你好啊\",\"url\":\"6.jpeg\"},{\"title\":\"是很好\",\"url\":\"7.jpeg\"}]}"
;
nsdictionary *infodic = [self dictionarywithjsonstring:string];
return
infodic;
}
-(nsdictionary *)dictionarywithjsonstring:(nsstring *)jsonstring {
if
(jsonstring == nil) {
return
nil;
}
nsdata *jsondata = [jsonstring datausingencoding:nsutf8stringencoding];
nserror *err;
nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:&err];
if
(err)
{
nslog(@
"json解析失败:%@"
,err);
return
nil;
}
return
dic;
}
- (nsmutablearray *)modelarray {
if
(!_modelarray) {
_modelarray = [nsmutablearray array];
}
return
_modelarray;
}
- (
void
)createcollectionview{
uicollectionviewflowlayout * layout = [[uicollectionviewflowlayout alloc]init];
layout.scrolldirection = uicollectionviewscrolldirectionhorizontal;
layout.minimumlinespacing = 0;
layout.minimuminteritemspacing = 0;
_collectionview = [[uicollectionview alloc] initwithframe:cgrectmake(0, 100, [uiscreen mainscreen].bounds.size.width, imageheight * screen_rate) collectionviewlayout:layout];
_collectionview.tag = 11;
_collectionview.backgroundcolor = [uicolor colorwithred:186 / 255.0 green:186 / 255.0 blue:186 / 255.0 alpha:0.9];
_collectionview.datasource = self;
_collectionview.delegate = self;
_collectionview.bounces = no;
_collectionview.alwaysbouncehorizontal = yes;
_collectionview.alwaysbouncevertical = no;
_collectionview.showshorizontalscrollindicator = no;
_collectionview.showsverticalscrollindicator = no;
[self.view addsubview:_collectionview];
[_collectionview registerclass:[imagecell
class
] forcellwithreuseidentifier:imagec];
}
- (
void
)createrightcollectionview{
lhhorizontalpageflowlayout * layout = [[lhhorizontalpageflowlayout alloc] initwithrowcount:krowcount itemcountperrow:kitemcountperrow];
[layout setcolumnspacing:0 rowspacing:0 edgeinsets:uiedgeinsetsmake(0, 0, 0, 0)];
layout.scrolldirection = uicollectionviewscrolldirectionhorizontal;
// uicollectionviewflowlayout *layout = [[uicollectionviewflowlayout alloc]init];
// layout.scrolldirection = uicollectionviewscrolldirectionhorizontal;
layout.minimumlinespacing = 0;
layout.minimuminteritemspacing = 0;
_morecollectionview = [[uicollectionview alloc] initwithframe:cgrectmake(0, 300, [uiscreen mainscreen].bounds.size.width, imageheight * screen_rate * krowcount) collectionviewlayout:layout];
_morecollectionview.backgroundcolor = [uicolor clearcolor];
_morecollectionview.tag = 22;
_morecollectionview.datasource = self;
_morecollectionview.delegate = self;
_morecollectionview.bounces = no;
_morecollectionview.alwaysbouncehorizontal = yes;
_morecollectionview.alwaysbouncevertical = no;
_morecollectionview.backgroundcolor = [uicolor colorwithred:186 / 255.0 green:186 / 255.0 blue:186 / 255.0 alpha:0.9];
_morecollectionview.showshorizontalscrollindicator = no;
_morecollectionview.showsverticalscrollindicator = no;
[self.view addsubview:_morecollectionview];
[_morecollectionview registerclass:[imagecell
class
] forcellwithreuseidentifier:moreimagec];
}
- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{
return
self.modelarray.count;
}
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {
collmodel * model = self.modelarray[indexpath.row];
imagecell * cell = [collectionview dequeuereusablecellwithreuseidentifier:imagec forindexpath:indexpath];
cell.itemmodel = model;
return
cell;
}
// 返回每个item的大小
- (cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath {
cgfloat cwidth = imageheight * screen_rate;
cgfloat cheight = imageheight * screen_rate;
return
cgsizemake(cwidth, cheight);
}
#pragma mark - uicollectionviewdelegate点击事件
- (
void
)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath{
collmodel * model = self.modelarray[indexpath.row];
nslog(@
"self.appmodelarray----%@"
,model.title);
}
- (
void
)didreceivememorywarning {
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
@end
|
2、自定义uicollectionviewflowlayout 。
lhhorizontalpageflowlayout.h 实现 。
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
|
#import <uikit/uikit.h>
@interface lhhorizontalpageflowlayout : uicollectionviewflowlayout
/** 列间距 */
@property (nonatomic, assign) cgfloat columnspacing;
/** 行间距 */
@property (nonatomic, assign) cgfloat rowspacing;
/** collectionview的内边距 */
@property (nonatomic, assign) uiedgeinsets edgeinsets;
/** 多少行 */
@property (nonatomic, assign) nsinteger rowcount;
/** 每行展示多少个item */
@property (nonatomic, assign) nsinteger itemcountperrow;
//固定宽度
@property (nonatomic, assign) cgfloat itemwidth;
//设置完这个,就会自动计算列间距
//固定高度
@property (nonatomic, assign) cgfloat itemhight;
//设置完这个,就会自动计算行间距
/** 所有item的属性数组 */
@property (nonatomic, strong) nsmutablearray *attributesarraym;
/** 设置行列间距及collectionview的内边距 */
- (
void
)setcolumnspacing:(cgfloat)columnspacing rowspacing:(cgfloat)rowspacing edgeinsets:(uiedgeinsets)edgeinsets;
/** 设置多少行及每行展示的item个数 */
- (
void
)setrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow;
#pragma mark - 构造方法
/** 设置多少行及每行展示的item个数 */
+ (instancetype)horizontalpageflowlayoutwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow;
/** 设置多少行及每行展示的item个数 */
- (instancetype)initwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow;
@end
|
lhhorizontalpageflowlayout.m 实现 。
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#import "lhhorizontalpageflowlayout.h"
@implementation lhhorizontalpageflowlayout
#pragma mark - public
- (
void
)setcolumnspacing:(cgfloat)columnspacing rowspacing:(cgfloat)rowspacing edgeinsets:(uiedgeinsets)edgeinsets
{
self.columnspacing = columnspacing;
self.rowspacing = rowspacing;
self.edgeinsets = edgeinsets;
}
- (
void
)setrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow
{
self.rowcount = rowcount;
self.itemcountperrow = itemcountperrow;
}
#pragma mark - 构造方法
+ (instancetype)horizontalpageflowlayoutwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow
{
return
[[self alloc] initwithrowcount:rowcount itemcountperrow:itemcountperrow];
}
- (instancetype)initwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow
{
self = [super init];
if
(self) {
self.rowcount = rowcount;
self.itemcountperrow = itemcountperrow;
}
return
self;
}
#pragma mark - 重写父类方法
- (instancetype)init
{
self = [super init];
if
(self) {
[self setcolumnspacing:0 rowspacing:0 edgeinsets:uiedgeinsetszero];
}
return
self;
}
/** 布局前做一些准备工作 */
- (
void
)preparelayout
{
[super preparelayout];
if
(self.attributesarraym && self.attributesarraym.count > 0) {
[self.attributesarraym removeallobjects];
}
// 从collectionview中获取到有多少个item
nsinteger itemtotalcount = [self.collectionview numberofitemsinsection:0];
// 遍历出item的attributes,把它添加到管理它的属性数组中去
for
(
int
i = 0; i < itemtotalcount; i++) {
nsindexpath *indexpath = [nsindexpath indexpathforitem:i insection:0];
uicollectionviewlayoutattributes *attributes = [self layoutattributesforitematindexpath:indexpath];
[self.attributesarraym addobject:attributes];
}
}
/** 计算collectionview的滚动范围 */
- (cgsize)collectionviewcontentsize
{
// 计算出item的宽度
cgfloat itemwidth = (self.collectionview.frame.size.width - self.edgeinsets.left - self.itemcountperrow * self.columnspacing) / self.itemcountperrow;
// 从collectionview中获取到有多少个item
nsinteger itemtotalcount = [self.collectionview numberofitemsinsection:0];
// 理论上每页展示的item数目
nsinteger itemcount = self.rowcount * self.itemcountperrow;
// 余数(用于确定最后一页展示的item个数)
nsinteger remainder = itemtotalcount % itemcount;
// 除数(用于判断页数)
nsinteger pagenumber = itemtotalcount / itemcount;
// 总个数小于self.rowcount * self.itemcountperrow
if
(itemtotalcount <= itemcount) {
pagenumber = 1;
}
else
{
if
(remainder == 0) {
pagenumber = pagenumber;
}
else
{
// 余数不为0,除数加1
pagenumber = pagenumber + 1;
}
}
cgfloat width = 0;
// 考虑特殊情况(当item的总个数不是self.rowcount * self.itemcountperrow的整数倍,并且余数小于每行展示的个数的时候)
if
(pagenumber > 1 && remainder != 0 && remainder < self.itemcountperrow) {
width = self.edgeinsets.left + (pagenumber - 1) * self.itemcountperrow * (itemwidth + self.columnspacing) + remainder * itemwidth + (remainder - 1)*self.columnspacing + self.edgeinsets.right;
}
else
{
width = self.edgeinsets.left + pagenumber * self.itemcountperrow * (itemwidth + self.columnspacing) - self.columnspacing + self.edgeinsets.right;
}
// 只支持水平方向上的滚动
return
cgsizemake(width, 150);
}
/** 设置每个item的属性(主要是frame) */
- (uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath
{
// item的宽高由行列间距和collectionview的内边距决定
cgfloat itemwidth = (self.collectionview.frame.size.width) / self.itemcountperrow;
cgfloat itemheight = (self.collectionview.frame.size.height) / self.rowcount;
nsinteger item = indexpath.item;
// 当前item所在的页
nsinteger pagenumber = item / (self.rowcount * self.itemcountperrow);
nsinteger x = item % self.itemcountperrow + pagenumber * self.itemcountperrow;
nsinteger y = item / self.itemcountperrow - pagenumber * self.rowcount;
// 计算出item的坐标
cgfloat itemx = itemwidth * x;
cgfloat itemy = itemheight * y;
uicollectionviewlayoutattributes *attributes = [super layoutattributesforitematindexpath:indexpath];
// 每个item的frame
attributes.frame = cgrectmake(itemx, itemy, itemwidth, itemheight);
return
attributes;
}
/** 返回collectionview视图中所有视图的属性数组 */
- (nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect
{
return
self.attributesarraym;
}
#pragma mark - lazy
- (nsmutablearray *)attributesarraym
{
if
(!_attributesarraym) {
_attributesarraym = [nsmutablearray array];
}
return
_attributesarraym;
}
@end
|
4、自定义cell 和model 。
model 。
1
2
3
4
5
6
7
|
#import <foundation/foundation.h>
@interface collmodel : nsobject
@property (nonatomic,strong)nsstring *imgurl;
@property (nonatomic,strong)nsstring *title;
@property (nonatomic,strong)nsstring *url;
@end
|
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
|
#import "imagecell.h"
// 屏幕比例
#define screen_rate ([uiscreen mainscreen].bounds.size.width/375.0)
@interface imagecell()
@property (nonatomic, strong) uiimageview *itemicon;
@end
@implementation imagecell
@synthesize itemmodel = _itemmodel;
- (instancetype)initwithframe:(cgrect)frame{
if
(self = [super initwithframe:frame]) {
self.contentview.backgroundcolor = [uicolor clearcolor];
[self initview];
}
return
self;
}
- (
void
)initview{
_itemicon = [[uiimageview alloc] init];
[self.contentview addsubview:_itemicon];
_itemicon.backgroundcolor = [uicolor clearcolor];
cgfloat iconwidth = 80 * screen_rate;
_itemicon.frame = cgrectmake(0, 0, iconwidth, iconwidth);
_itemicon.center = self.contentview.center;
}
- (collmodel *)itemmodel{
return
_itemmodel;
}
- (
void
)setitemmodel:(collmodel *)itemmodel
{
if
(!itemmodel) {
return
;
}
_itemmodel = itemmodel;
[self setcellwithmodel:_itemmodel];
}
- (
void
)setcellwithmodel:(collmodel *)itemmodel{
[[nsoperationqueue mainqueue] addoperationwithblock:^{
_itemicon.image = [uiimage imagenamed:itemmodel.url];
}];
}
|
github下载 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/u013983033/article/details/83095126 。
最后此篇关于ios uicollectionview实现横向滚动的文章就讲到这里了,如果你想了解更多关于ios uicollectionview实现横向滚动的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
如果附加了 'not-scroll' 类,我希望我的 body 不滚动,否则它应该正常工作。 我已经搜索这个问题两天了,但找不到任何适合我的解决方案。 我想要的是向 body 添加一个 class,并
我发现似乎是 iOS Safari 中的错误(我正在 iOS 8 上进行测试)。当绝对定位的 iFrame 漂浮在一段可滚动内容上方时,滚动 iFrame 也会滚动下面的内容。以下 HTML (ava
我有以下代码来显示一系列投资组合图片,这些图片以 SVG 格式存储在滚动 div 中: 在 Safari 中滚动使用两根手指或鼠标滚轮当光标位于 SVG 之一上时不起作用。 该页
我想用 javascript 做的是: 一旦你向下滚动页面,将#sidebar-box-fixed 的位置从 position: relative; 更改为定位:固定;。改回position:rela
我对 Elasticsearch 的滚动功能有点困惑。在 elasticsearch 中,每当用户在结果集上滚动时,是否可以每次调用搜索 API?来自文档 "search_type" => "scan
我试图做到这一点,以便当我向上或向下滚动页面时,它会运行不同的相应功能。我发现了一个类似的问题here但我已经尝试了他们的答案并且没有运气。 注意:此页面没有正常显示的滚动条。没有地方可以滚动。 bo
(C语言,GTK库) 在我的表单上,我有一个 GtkDrawingArea 小部件,我在上面使用 Cairo 绘制 GdkPixbufs(从文件加载)。我想要完成的是能够在窗口大小保持固定的情况下使用
最近我一直在尝试创建一个拉到(刷新,加载更多)swiftUI ScrollView !!,灵感来自 https://cocoapods.org/pods/SwiftPullToRefresh 我正在努
我正在开发一个应用程序,其中有两个带有可放置区域的列表和一个带有可拖动项目的侧面菜单。 当我滚动屏幕时,项目的位置困惑。 我试图在谷歌上寻找一些东西,最后得到了这个问题:jQuery draggabl
我在 UIWebView 中加载了一个 HTML 表单,而我的 UIWebView 恰好从 View 的中间开始并扩展。我必须锁定此 webView 不滚动并将其放在 ScrollView 之上以允许
如何在每个元素而不是整个元素上应用淡入淡出(与其高度相比)? HTML: CSS: * { padding: 0; margin: 0; box-sizing: border
我想使用带有垂直轴的 PageView 并使用鼠标滚动在页面之间移动,但是当我使用鼠标滚动时页面不滚动...仅页面单击并向上/向下滑动时滚动。 有什么办法吗? 我想保留属性 pageSnapping:
我制作这个程序是为了好玩,但我被卡住了,因为程序在屏幕外运行。如何在不完全更改代码的情况下实现滚动条。 public static void main(String args[]) throws IO
我想使用带有垂直轴的 PageView 并使用鼠标滚动在页面之间移动,但是当我使用鼠标滚动时页面不滚动...仅页面单击并向上/向下滑动时滚动。 有什么办法吗? 我想保留属性 pageSnapping:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
使用 jquery 技术从 css-tricks.com 获得滚动/跟随侧边栏,如果您不知道我在说什么,这里是代码: $(function() { var $sidebar = $
我是 jQuery Mobile 新手。我需要向我的应用程序添加 Facebook 滑动面板功能。 我经历了 sliding menu panel ,它工作正常,但我在菜单面板中的内容超出了窗口大小,
有没有办法在 js 或 jQuery 或任何其他工具中检测 ctrl + 滚动。我正在尝试执行一些动态布局代码,我需要检测不同分辨率下的屏幕宽度,我通过使用 setTimeout() 的计时器实现了这
我有一部分html代码:
我想控制 RichTextBox 滚动,但在控件中找不到任何方法来执行此操作。 这样做的原因是我希望当鼠标光标位于 RichTextBox 控件上时鼠标滚轮滚动有效(它没有事件焦点:鼠标滚轮事件由表单
我是一名优秀的程序员,十分优秀!