iOS开发UI篇—无限轮播(功能完善)

简介: iOS开发UI篇—无限轮播(功能完善) 一、自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条。   获取当前正在展示的位置。 1 [self addNSTimer]; 2 } 3 4 -(void)addNSTimer 5 { 6 // ...

iOS开发UI篇—无限轮播(功能完善)

一、自动滚动

添加并设置一个定时器,每个2.0秒,就跳转到下一条。

  获取当前正在展示的位置。

 1     [self addNSTimer];
 2 }
 3 
 4 -(void)addNSTimer
 5 {
 6 //    NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>
 7     [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
 8 }
 9 -(void)nextPage
10 {
11     NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
12 //    NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
13 }

  打印查看:

  

实现步骤:

(1)添加并设置定时器

(2)设置定时器的调用方法

  1)获取当前正在展示的位置

  2)计算出下一个需要展示的位置

  3)通过动画滚动到下一个位置

  注意点:需要进行判断。

实现代码:

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     //注册cell
 5 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
 6     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
 7     
 8     
 9      [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
10      [self addNSTimer];
11 }
12 
13 -(void)addNSTimer
14 {
15 //    NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>
16     
17    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
18     //添加到runloop中
19     [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
20 }
21 -(void)nextPage
22 {
23 //    NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
24         //1)获取当前正在展示的位置
25     NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
26     
27        //2)计算出下一个需要展示的位置
28     NSInteger nextItem=currentIndexPath.item+1;
29     NSInteger nextSection=currentIndexPath.section;
30     if (nextItem==self.news.count) {
31         nextItem=0;
32         nextSection++;
33     }
34     NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
35     
36       //3)通过动画滚动到下一个位置
37      [self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
38 }

定时器的说明:

  当用户在处理其他事情的时候,定时器会停止工作。应该把定时器添加到runloop中,告诉系统在处理其他事情的时候分一部分空间给它。

 

二、设置页码

  在storyboard中添加一个页码控件。

实现代码:  

 1 #pragma mark-懒加载
 2 -(NSArray *)news
 3 {
 4     if (_news==nil) {
 5         _news=[YYnews objectArrayWithFilename:@"newses.plist"];
 6         self.pageControl.numberOfPages=self.news.count;
 7     }
 8     return _news;
 9 }
10 
11 - (void)viewDidLoad
12 {
13     [super viewDidLoad];
14     //注册cell
15 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
16     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
17     
18     
19      [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
20     [self addNSTimer];
21 }
22 
23 -(void)addNSTimer
24 {
25 //    NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>
26     
27    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
28     //添加到runloop中
29     [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
30 }
31 -(void)nextPage
32 {
33 //    NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
34         //1)获取当前正在展示的位置
35     NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
36     
37        //2)计算出下一个需要展示的位置
38     NSInteger nextItem=currentIndexPath.item+1;
39     NSInteger nextSection=currentIndexPath.section;
40     if (nextItem==self.news.count) {
41         nextItem=0;
42         nextSection++;
43     }
44     NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
45     
46       //3)通过动画滚动到下一个位置
47      [self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
48     
49     //4)设置页码
50     self.pageControl.currentPage=nextItem;
51 }

 自动滚动,页码的实现效果:

三、完善

说明:监听collectionView的滚动,当手动触摸collectionView,尝试拖拽的时候,把定时器停掉,当手指移开的时候,重启定时器。

完整的实现代码:

  1 //
  2 //  YYViewController.m
  3 //  07-无限滚动(循环利用)
  4 //
  5 //  Created by apple on 14-8-3.
  6 //  Copyright (c) 2014年 yangyong. All rights reserved.
  7 //
  8 
  9 #import "YYViewController.h"
 10 #import "MJExtension.h"
 11 #import "YYnews.h"
 12 #import "YYcell.h"
 13 
 14 #define YYIDCell @"cell"
 15 //注意:这里需要考虑用户的手动拖拽
 16 #define YYMaxSections 100
 17 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
 18 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
 19 @property(nonatomic,strong)NSArray *news;
 20 @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
 21 @property(nonatomic,strong)NSTimer *timer;
 22 
 23 @end
 24 
 25 @implementation YYViewController
 26 
 27 #pragma mark-懒加载
 28 -(NSArray *)news
 29 {
 30     if (_news==nil) {
 31         _news=[YYnews objectArrayWithFilename:@"newses.plist"];
 32         self.pageControl.numberOfPages=self.news.count;
 33     }
 34     return _news;
 35 }
 36 
 37 - (void)viewDidLoad
 38 {
 39     [super viewDidLoad];
 40     //注册cell
 41 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
 42     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
 43     
 44     
 45      [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
 46     [self addNSTimer];
 47 }
 48 
 49 //添加定时器
 50 -(void)addNSTimer
 51 {
 52    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
 53     //添加到runloop中
 54     [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
 55     self.timer=timer;
 56 }
 57 
 58 //删除定时器
 59 -(void)removeNSTimer
 60 {
 61     [self.timer invalidate];
 62     self.timer=nil;
 63 }
 64 
 65 //自动滚动
 66 -(void)nextPage
 67 {
 68         //1获取当前正在展示的位置
 69     NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
 70     
 71     //马上显示回最中间那组的数据
 72     NSIndexPath *currentIndexPathRest=[NSIndexPath indexPathForItem:currentIndexPath.item inSection:YYMaxSections/2];
 73     [self.collectinView scrollToItemAtIndexPath:currentIndexPathRest atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
 74     
 75        //2计算出下一个需要展示的位置
 76     NSInteger nextItem=currentIndexPathRest.item+1;
 77     NSInteger nextSection=currentIndexPathRest.section;
 78     if (nextItem==self.news.count) {
 79         nextItem=0;
 80         nextSection++;
 81     }
 82     NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
 83     
 84       //3通过动画滚动到下一个位置
 85      [self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
 86     
 87 //    //4)设置页码
 88 //    self.pageControl.currentPage=nextItem;
 89 }
 90 
 91 #pragma mark- UICollectionViewDataSource
 92 //一共多少组,默认为1组
 93 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
 94 {
 95     return YYMaxSections;
 96 }
 97 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 98 {
 99     return self.news.count;
100 }
101 
102 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
103 {
104     YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
105     cell.news=self.news[indexPath.item];
106     NSLog(@"%p,%d",cell,indexPath.item);
107     return cell;
108 }
109 
110 #pragma mark-UICollectionViewDelegate
111 //当用户开始拖拽的时候就调用
112 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
113 {
114     [self removeNSTimer];
115 }
116 //当用户停止拖拽的时候调用
117 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
118 {
119     [self addNSTimer];
120 }
121 //设置页码
122 -(void)scrollViewDidScroll:(UIScrollView *)scrollView
123 {
124     int page=(int)(scrollView.contentOffset.x/scrollView.frame.size.width+0.5)%self.news.count;
125     self.pageControl.currentPage=page;
126 }
127 @end

补充说明:

  实现瀑布流最理想的做法是继承UIScrollView,不建议使用多个UITableView的方式实现(这种方式无法实现cell的循环利用,且禁止了cell的滚动时间后,整体的tableView需要随着滚动会出现空白)。

  也可以使用collectionView来实现,但使用这种方法需要自定义collectionView的布局(流水布局)

目录
相关文章
|
开发框架 前端开发 JavaScript
【HarmonyOS Next之旅】基于ArkTS开发(二) -> UI开发一
本文介绍了方舟开发框架(ArkUI)及其两种开发范式:基于ArkTS的声明式开发范式和类Web开发范式。ArkUI是用于构建HarmonyOS应用界面的UI框架,提供极简UI语法和基础设施。声明式开发范式使用ArkTS语言,以组件、动画和状态管理为核心,适合复杂团队协作;类Web开发范式采用HML、CSS、JavaScript三段式开发,适用于简单界面应用,贴近Web开发者习惯。文中还概述了两者的架构和基础能力,帮助开发者选择合适的范式进行高效开发。
496 15
|
编解码 前端开发 Java
【HarmonyOS Next之旅】基于ArkTS开发(二) -> UI开发三
本文介绍了基于声明式UI范式的图形绘制与动画效果实现方法,涵盖绘制图形、添加动画效果及常见组件说明三部分内容。在绘制图形部分,详细讲解了如何通过Circle组件为食物成分表添加圆形标签,以及使用Path组件结合SVG命令绘制自定义图形(如应用Logo)。动画效果部分则展示了如何利用animateTo实现闪屏动画,包括渐出、放大效果,并设置页面跳转;同时介绍了页面间共享元素转场动画的实现方式。最后,文章列举了声明式开发范式中的各类组件及其功能,帮助开发者快速上手构建复杂交互页面。
484 11
|
11月前
|
存储 开发者 容器
鸿蒙 HarmonyOS NEXT星河版APP应用开发-ArkTS面向对象及组件化UI开发使用实例
本文介绍了ArkTS语言中的Class类、泛型、接口、模块化、自定义组件及状态管理等核心概念,并结合代码示例讲解了对象属性、构造方法、继承、静态成员、访问修饰符等内容,同时涵盖了路由管理、生命周期和Stage模型等应用开发关键知识点。
686 1
鸿蒙 HarmonyOS NEXT星河版APP应用开发-ArkTS面向对象及组件化UI开发使用实例
|
JavaScript 前端开发 UED
【HarmonyOS Next之旅】基于ArkTS开发(二) -> UI开发四
本文介绍了Web组件开发与性能优化的相关内容。在Web组件开发部分,涵盖创建组件、设置样式与属性、添加事件和方法以及场景示例,如动态播放视频。性能提升方面,推荐使用数据懒加载、条件渲染替代显隐控制、Column/Row替代Flex、设置List组件宽高及调整cachedCount减少滑动白块等方法,以优化应用性能与用户体验。
395 56
|
编解码 UED 开发者
【HarmonyOS Next之旅】基于ArkTS开发(二) -> UI开发之常见布局
本文主要介绍了自适应布局与响应式布局的相关内容。自适应布局部分涵盖线性布局、层叠布局、弹性布局和网格布局,详细说明了各布局的特性及使用方法,例如线性布局中的排列、拉伸与缩放,弹性布局的方向、换行与对齐方式等。响应式布局则重点讲解了栅格系统和媒体查询,阐述如何通过栅格组件和媒体查询条件实现不同设备上的适配效果。这些技术帮助开发者灵活应对多尺寸屏幕的设计需求,提升用户体验。
641 55
|
存储 开发框架 API
【HarmonyOS Next之旅】基于ArkTS开发(二) -> UI开发二
本文详细介绍了基于声明式UI开发的健康饮食应用的设计与实现过程。内容涵盖从基础环境搭建到复杂功能实现的全流程,包括创建简单视图、构建布局(如Stack、Flex)、数据模型设计、列表与网格布局构建,以及页面跳转和数据传递等核心功能。 本文通过实际案例深入浅出地解析了声明式UI开发的关键技术和最佳实践,为开发者提供了宝贵的参考。
529 14
|
JavaScript 前端开发 开发者
09.HarmonyOS Next数据驱动UI开发:ForEach与动态渲染完全指南(上)
在现代前端开发中,数据驱动UI已成为主流开发范式。HarmonyOS Next的ArkTS语言和声明式UI框架完美支持这一理念,使开发者能够以更高效、更直观的方式构建复杂应用。
342 1
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
iOS开发 开发者
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
1037 67
uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
559 66