服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现卡片式滚动效果 iOS实现电影选片效果

iOS实现卡片式滚动效果 iOS实现电影选片效果

2021-05-30 23:13iMoonT IOS

这篇文章主要为大家详细介绍了iOS实现卡片式滚动效果,实现电影选片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ios实现卡片式滚动效果的具体代码,供大家参考,具体内容如下

先来张效果图吧:

iOS实现卡片式滚动效果 iOS实现电影选片效果

直接上源码了:

cardscrollview.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import <uikit/uikit.h>
 
@interface cardview : uiview
 
@property (nonatomic, assign) cgfloat zoomrate;
 
@property (nonatomic, strong) nsstring *imgurl;
 
- (uiimage *)getimage;
 
@end
 
@interface cardscrollview : uiview
 
@property (nonatomic, assign) cgfloat cardviewwidth;
@property (nonatomic, assign) cgfloat mincardzoomrate;
@property (nonatomic, assign) cgfloat maxcardzoomrate;
 
@property (nonatomic, assign) bool needbackgroundblurimage;
 
- (void)setimgurls:(nsarray<nsstring *> *)imgurls selectedcard:(void(^)(nsinteger selectedindex))selectedcard;
 
@end

cardscrollview.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#import "cardscrollview.h"
 
@interface cardview ()
 
@property (nonatomic, strong) uiimageview *imgview;
 
@end
 
@implementation cardview
 
- (instancetype)initwithframe:(cgrect)frame {
 if (self = [super initwithframe:frame]) {
 _imgview = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, cgrectgetwidth(frame), cgrectgetheight(frame))];
 [_imgview setcontentmode:uiviewcontentmodescaleaspectfit];
 [_imgview setclipstobounds:yes];
 [self addsubview:_imgview];
 }
 return self;
}
 
- (void)setzoomrate:(cgfloat)zoomrate {
 if (zoomrate < 0) {
 zoomrate = 0;
 }
 _zoomrate = zoomrate;
 cgfloat width = cgrectgetwidth(self.bounds);
 cgfloat height = cgrectgetheight(self.bounds);
 cgfloat imgviewwidth = width * zoomrate;
 cgfloat imgviewheight = height * zoomrate;
 _imgview.frame = cgrectmake((width - imgviewwidth) / 2, (height - imgviewheight) / 2, imgviewwidth, imgviewheight);
}
 
- (void)setimgurl:(nsstring *)imgurl {
 _imgurl = imgurl;
 [_imgview setimage:[uiimage imagenamed:imgurl]];
}
 
- (uiimage *)getimage {
 return [_imgview image];
}
 
@end
 
@interface cardscrollview () <uiscrollviewdelegate>
 
@property (nonatomic, strong) uiimageview *bgimageview;
@property (nonatomic, strong) uivisualeffectview *blurview;
 
@property (nonatomic, strong) uiscrollview *scrollview;
@property (nonatomic, assign) cgfloat aroundspacing;
@property (nonatomic, strong) nsarray<nsstring *> *imgurls;
@property (nonatomic, strong) nsmutablearray<cardview *> *cardviewarray;
@property (nonatomic, assign) nsinteger currentindex;
 
@property (nonatomic, strong) void(^selectedcard)(nsinteger);
 
@end
 
@implementation cardscrollview
 
- (uiimageview *)bgimageview {
 if (!_bgimageview) {
 _bgimageview = [[uiimageview alloc] initwithframe:self.bounds];
 }
 return _bgimageview;
}
 
 
- (uivisualeffectview *)blurview {
 if (!_blurview) {
 [self addsubview:self.bgimageview];
 _blurview = [[uivisualeffectview alloc] initwithframe:self.bounds];
 [_blurview seteffect:[uiblureffect effectwithstyle:uiblureffectstylelight]];
 }
 return _blurview;
}
 
- (uiscrollview *)scrollview {
 if (!_scrollview) {
 _scrollview = [[uiscrollview alloc] initwithframe:self.bounds];
 _scrollview.delegate = self;
 [_scrollview setshowshorizontalscrollindicator:no];
 }
 return _scrollview;
}
 
- (nsmutablearray<cardview *> *)cardviewarray {
 if (!_cardviewarray) {
 _cardviewarray = [nsmutablearray array];
 }
 return _cardviewarray;
}
 
- (void)layoutsubviews {
 [super layoutsubviews];
 if (_needbackgroundblurimage) {
 [self addsubview:self.blurview];
 }
 [self addsubview:self.scrollview];
}
 
- (void)setimgurls:(nsarray<nsstring *> *)imgurls selectedcard:(void (^)(nsinteger))selectedcard {
 _imgurls = imgurls;
 _selectedcard = selectedcard;
 [self layoutcardviews];
}
 
- (void)layoutcardviews {
 if (_cardviewwidth == 0) {
 _cardviewwidth = cgrectgetwidth(self.bounds) / 2;
 }
 if (_mincardzoomrate == 0) {
 _mincardzoomrate = 0.5;
 }
 if (_maxcardzoomrate == 0) {
 _maxcardzoomrate = 1;
 }
 _aroundspacing = (cgrectgetwidth(self.bounds) - _cardviewwidth) / 2;
 for (int i = 0; i < [_imgurls count]; i++) {
 cardview *cardview = [[cardview alloc] initwithframe:cgrectmake(_aroundspacing + i * (_cardviewwidth), 0, _cardviewwidth, cgrectgetheight(self.bounds))];
 cardview.zoomrate = _mincardzoomrate;
 cardview.imgurl = _imgurls[i];
 cardview.tag = i;
 uitapgesturerecognizer *tapgr = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(clickcardview:)];
 [cardview addgesturerecognizer:tapgr];
 [_scrollview addsubview:cardview];
 [self.cardviewarray addobject:cardview];
 }
 [_scrollview setcontentsize:cgsizemake(cgrectgetmaxx([_cardviewarray lastobject].frame) + _aroundspacing, 0)];
 [self setcardviewzoomrate:[_cardviewarray firstobject]];
 [self selectindex:0];
}
 
- (void)clickcardview:(uigesturerecognizer *)gesturecognizer {
 [self selectindex:gesturecognizer.view.tag];
}
 
- (void)selectindex:(nsinteger)index {
 _currentindex = index;
 cardview *cardview = _cardviewarray[index];
 [_scrollview setcontentoffset:cgpointmake(cgrectgetmidx(cardview.frame) - cgrectgetwidth(_scrollview.frame) / 2, 0) animated:yes];
 if (_needbackgroundblurimage) {
 [_bgimageview setimage:[cardview getimage]];
 }
 if (_selectedcard) {
 _selectedcard(index);
 }
}
 
#pragma mark - 根据 cardview 在 x 轴的中心坐标 设置其 zoomrate
- (void)setcardviewzoomrate:(cardview *)cardview {
 cgfloat offsetrate = abs(_scrollview.contentoffset.x + cgrectgetwidth(_scrollview.frame) / 2 - cgrectgetmidx(cardview.frame)) / _cardviewwidth;
 cgfloat zoomrate = _maxcardzoomrate - offsetrate;
 if (zoomrate < _mincardzoomrate) {
 zoomrate = _mincardzoomrate;
 }
 [_scrollview bringsubviewtofront:cardview];
 [cardview setzoomrate:zoomrate];
}
 
#pragma mark - uiscrollview delegate
- (void)scrollviewdidscroll:(uiscrollview *)scrollview {
 nsinteger index = floorf((scrollview.contentoffset.x - _aroundspacing + cgrectgetwidth(_scrollview.frame) / 2) / _cardviewwidth);
 if (index < 0) {
 index = 0;
 }
 if (index > [_cardviewarray count] - 1) {
 index = [_cardviewarray count];
 }
 [self setcardviewzoomrate:_cardviewarray[index]];
}
 
- (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview {
 int index = (scrollview.contentoffset.x - _aroundspacing + cgrectgetwidth(_scrollview.frame) / 2) / _cardviewwidth;
 [self selectindex:index];
}
 
- (void)scrollviewdidenddragging:(uiscrollview *)scrollview willdecelerate:(bool)decelerate {
 if (!decelerate) {
 [self scrollviewdidenddecelerating:scrollview];
 }
}
 
@end

使用:viewcontroller.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
#import "viewcontroller.h"
#import "cardscrollview.h"
 
@interface viewcontroller ()
 
@property (weak, nonatomic) iboutlet cardscrollview *cardscrollview;
//@property (nonatomic, strong) cardscrollview *cardscrollview;
 
@property (nonatomic, strong) nsmutablearray<nsstring *> *imgurls;
 
@end
 
@implementation viewcontroller
 
- (nsmutablearray<nsstring *> *)imgurls {
 if (!_imgurls) {
 _imgurls = [nsmutablearray array];
 for (int i = 0; i < 9; i++) {
  [_imgurls addobject:[nsstring stringwithformat:@"%d", i + 1]];
 }
 }
 return _imgurls;
}
 
//- (cardscrollview *)cardscrollview {
// if (!_cardscrollview) {
// _cardscrollview = [[cardscrollview alloc] initwithframe:self.view.bounds];
// _cardscrollview.cardviewwidth = 150;
// _cardscrollview.needbackgroundblurimage = yes;
// }
// return _cardscrollview;
//}
 
- (void)viewdidappear:(bool)animated {
 [super viewdidappear:animated];
// [self.view addsubview:self.cardscrollview];
 [_cardscrollview setimgurls:self.imgurls selectedcard:^(nsinteger selectedindex) {
 
 }];
}
 
@end

我一般习惯storyboard开发,所以这里就使用的storyboard拖拽的,代码中注释掉的 是纯代码使用的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/CBlog_life/article/details/78587033

延伸 · 阅读

精彩推荐
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01