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

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

服务器之家 - 编程语言 - IOS - IOS 仿时光网选票UI实例代码

IOS 仿时光网选票UI实例代码

2021-01-29 15:43康大亮 IOS

这篇文章主要介绍了IOS 仿时光网选票UI实例代码的相关资料,需要的朋友可以参考下

一、项目简介

该项目利用uiscrollview的各种滚动事件的监听,仿造时光网选择电影票的ui而开发的一个自定义view。使用简单,可扩展性很强。具备点击每个item进行选票功能,选票居中功能,滑动时自动选择距离中间最近的view处于选中状态,而且对于滑动时松开手的时候是否有初始速度进行了区分处理。案例演示如下:<br/>

IOS 仿时光网选票UI实例代码

仿时光网选票ui

二、项目讲解

1、初始化uiscrollview中每个item的view,把每个view放到_viewarray数组中,方便接下来的定位和管理。每一个view中包含一个uiimageview,把每一个uiimageview放在_imageviewarray数组中,方便接下来的进行随着滑动的放大和缩小操作。

?
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
-(instancetype)initviewwithimagearray:(nsarray *)imagearray{
if (!imagearray) {
return nil;
}
if (imagearray.count<1) {
return nil;
}
 
nsinteger totalnum = imagearray.count;
self = [super initwithframe:cgrectmake(0, 40, screen_width, 120)];
if (self) {
_scrollview = [[uiscrollview alloc] initwithframe:self.bounds];
_scrollview.contentsize = cgsizemake(left_space*2+select_view_width+(totalnum-1)*normal_view_width+(totalnum-1)*item_space, 120);
_scrollview.delegate = self;
_scrollview.showshorizontalscrollindicator = no;
_scrollview.decelerationrate = uiscrollviewdecelerationratefast;
[self addsubview:_scrollview];
 
uiview *backview = [[uiview alloc] initwithframe:cgrectmake(-screen_width, 0, _scrollview.contentsize.width+screen_width*2, _scrollview.contentsize.height-20)];
backview.backgroundcolor = [uicolor lightgraycolor];
[_scrollview addsubview:backview];
 
_imageviewarray = [nsmutablearray array];
_viewarray = [nsmutablearray array];
 
cgfloat offsetx = left_space;
for (int i=0; i<totalnum; i++) {
 
uiview *view = [[uiview alloc] initwithframe:cgrectmake(offsetx, 0, normal_view_width, normal_view_height)];
[_scrollview addsubview:view];
[_viewarray addobject:view];
offsetx += normal_view_width+item_space;
 
 
cgrect rect;
if (i==0) {
rect = cgrectmake(-(select_view_width-normal_view_width)/2, 0, select_view_width, select_view_height);
}else{
rect = cgrectmake(0, 0, normal_view_width, normal_view_height);
}
uiimageview *imageview = [[uiimageview alloc] initwithframe:rect];
imageview.image = imagearray[i];
imageview.tag = i;
imageview.userinteractionenabled = yes;
uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(clickimage:)];
[imageview addgesturerecognizer:tap];
[view addsubview:imageview];
[_imageviewarray addobject:imageview];
}
 
}
return self;
}

2、在滑动的过程中,我们实时的需要改变计算哪一个item距离中间最近,在过渡到最中间的过程中,选中的item距离中间越近,选中item的frame越大,反则越小。

?
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
-(void)scrollviewdidscroll:(uiscrollview *)scrollview{
int currentindex = scrollview.contentoffset.x/(normal_view_width+item_space);
if (currentindex>_imageviewarray.count-2||currentindex<0) {
return;
}
int rightindex = currentindex+1;
uiimageview *currentimageview = _imageviewarray[currentindex];
uiimageview *rightimageview = _imageviewarray[rightindex];
 
 
cgfloat scale = (scrollview.contentoffset.x-currentindex*(normal_view_width+item_space))/(normal_view_width+item_space);
 
//nslog(@"%f",scale);
 
cgfloat width = select_view_width-scale*(select_view_width-normal_view_width);
cgfloat height = select_view_height-scale*(select_view_height-normal_view_height);
if (width<normal_view_width) {
width = normal_view_width;
}
if (height<normal_view_height) {
height = normal_view_height;
}
if (width>select_view_width) {
width = select_view_width;
}
if (height>select_view_height) {
height = select_view_height;
}
cgrect rect = cgrectmake(-(width-normal_view_width)/2, 0, width, height);
currentimageview.frame = rect;
 
width = normal_view_width+scale*(select_view_width-normal_view_width);
height = normal_view_height+scale*(select_view_height-normal_view_height);
if (width<normal_view_width) {
width = normal_view_width;
}
if (height<normal_view_height) {
height = normal_view_height;
}
if (width>select_view_width) {
width = select_view_width;
}
if (height>select_view_height) {
height = select_view_height;
}
rect = cgrectmake(-(width-normal_view_width)/2, 0, width, height);
nslog(@"%@",nsstringfromcgrect(rect));
rightimageview.frame = rect;
}

3、点击某一个item,让item处于中间选中状态。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-(void)clickimage:(uitapgesturerecognizer *)tap{
uiimageview *imageview = (uiimageview *)tap.view;
nsinteger tag = imageview.tag;
 
uiview *containerview = _viewarray[tag];
 
cgfloat offsetx = cgrectgetmidx(containerview.frame)-screen_width/2;
 
 
[_scrollview scrollrecttovisible:cgrectmake(offsetx, 0, screen_width, 120) animated:yes];
 
if (_delegate && [_delegate respondstoselector:@selector(itemselected:)]) {
[_delegate itemselected:tag];
}
 
}

4、当用户在滑动结束,并具有初始速度的时候,当滑动停止的时候,我们需要把距离中间最近item定位到最中间。

?
1
2
3
4
5
6
7
8
9
-(void)scrollviewdidenddecelerating:(uiscrollview *)scrollview{
int currentindex = roundf(scrollview.contentoffset.x/(normal_view_width+item_space));
uiview *containerview = _viewarray[currentindex];
cgfloat offsetx = cgrectgetmidx(containerview.frame)-screen_width/2;
[_scrollview scrollrecttovisible:cgrectmake(offsetx, 0, screen_width, 120) animated:yes];
if (_delegate && [_delegate respondstoselector:@selector(itemselected:)]) {
[_delegate itemselected:currentindex];
}
}

5、当用户在滑动结束的时候,但是没有初始速度的时候,此时不会触发-(void)scrollviewdidenddecelerating:(uiscrollview )scrollview方法,我们需要在-(void)scrollviewdidenddragging:(uiscrollview )scrollview willdecelerate:(bool)decelerate方法中,进行处理。

?
1
2
3
4
5
6
7
8
9
10
11
-(void)scrollviewdidenddragging:(uiscrollview *)scrollview willdecelerate:(bool)decelerate{
if (!decelerate) {
int currentindex = roundf(scrollview.contentoffset.x/(normal_view_width+item_space));
uiview *containerview = _viewarray[currentindex];
cgfloat offsetx = cgrectgetmidx(containerview.frame)-screen_width/2;
[_scrollview scrollrecttovisible:cgrectmake(offsetx, 0, screen_width, 120) animated:yes];
if (_delegate && [_delegate respondstoselector:@selector(itemselected:)]) {
[_delegate itemselected:currentindex];
}
}
}

6、注意点,设置_scrollview.decelerationrate = uiscrollviewdecelerationratefast;减慢uiscrollview滑动速度。会使用户体验更好。

三、项目使用

1、本项目支持cocospod,引用工程代码如下:

pod 'yxfilmselectview', '~> 0.0.1'

2、使用方法

?
1
2
3
yxfilmselectview *filmselectview = [[yxfilmselectview alloc] initviewwithimagearray:imagearray];
filmselectview.delegate = self;
[self.view addsubview:filmselectview];

3、提供yxfilmselectviewdelegate代理,用于每一个item处于选中状态的处理。

?
1
2
3
4
- (void)itemselected:(nsinteger)index{
_containerview.backgroundcolor = _colorarray[index%_colorarray.count];
_showlabel.text = [nsstring stringwithformat:@"%zi",index];
}

四、demo下载地址

demo下载地址

以上就是ios 仿时光网选票ui实例,有需要的朋友可以参考下,谢谢大家对本站的支持!

原文链接:http://www.jianshu.com/p/1b5a2db5053f

延伸 · 阅读

精彩推荐
  • IOS关于iOS自适应cell行高的那些事儿

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

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

    daisy6092021-05-17
  • IOSiOS布局渲染之UIView方法的调用时机详解

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

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

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

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

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

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

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

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

    苦练内功5832021-04-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

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

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

    一片枫叶4662020-12-25
  • IOSiOS通过逆向理解Block的内存模型

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

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

    Swiftyper12832021-03-03
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

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

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

    xiari5772021-06-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

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

    SimpleWorld11022021-01-28