当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS 仿时光网选票UI实例代码

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

贡品仙姬,祝义方,妾家住横塘

一、项目简介

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


仿时光网选票ui

二、项目讲解

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

-(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越大,反则越小。

-(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处于中间选中状态。

-(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定位到最中间。

-(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方法中,进行处理。

-(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、使用方法

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

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

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

四、demo下载地址

demo下载地址

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网