这种实现方式的特点:有多少张图片就要创建多少个UIImageView控件。这种实现方式的特点:无论多少图片,最多只创建3个cell,省内存。当轮播图只有一张图片时,cell只创建一个。当轮播图两张及两张以上时,cell创建3个,图片在这3个cell之间复用。
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableView];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 500)];
tableView.tableHeaderView = headerView;
//用UICollectionView实现轮播
HRCycleView *cycleView = [[HRCycleView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 200)];
[headerView addSubview:cycleView];
//下拉刷新
tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
cycleView.data = @[@"num_1",@"num_2",@"num_3",@"num_4",@"num_5"];
[tableView.mj_header endRefreshing];
}];
[tableView.mj_header beginRefreshing];
HRCycleView事先创建好子控件UICollectionView和UIPageControl,提供data属性用于接收图片数组。
处理循环播放的关键在于collectionView的代理方法-collectionView:didEndDisplayingCell:forItemAtIndexPath:,当cell完全移出屏幕时,该方法会获得回调,可在该方法中及时调整contentOffSet,让用户永远无法感知到UICollectionView的左右边界,具体代码如下:
#import "HRCycleView.h"
#import "HRCollectionViewCell.h"
//轮播间隔
static CGFloat ScrollInterval = 3.0f;
@interface HRCycleView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSIndexPath *nextIndexPath;
@end
@implementation HRCycleView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_autoPage = YES;
[self buildUI];
}
return self;
}
static NSString *cellID = @"HRCollectionViewCell";
- (void)buildUI {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height);
layout.minimumLineSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.pagingEnabled = true;
self.collectionView.backgroundColor = [UIColor clearColor];
[self.collectionView registerClass:[HRCollectionViewCell class] forCellWithReuseIdentifier:cellID];
self.collectionView.showsHorizontalScrollIndicator = false;
[self addSubview:self.collectionView];
CGFloat controlHeight = 35.0f;
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - controlHeight, self.bounds.size.width, controlHeight)];
self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
[self addSubview:self.pageControl];
}
#pragma mark - CollectionViewDelegate&DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HRCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.imageName = self.dataArray[indexPath.row];
NSLog(@"offset.x:%.1f, cell地址:%p", collectionView.contentOffset.x, cell);
return cell;
}
//cell刚移入屏幕时回调,indexPath对应刚移入屏幕的cell下标
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
self.nextIndexPath = indexPath;
}
//cell完全移出屏幕时回调,indexPath对应完全移出屏幕的cell下标
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.nextIndexPath != indexPath) {
[self adjustScrollLocation];
}
}
#pragma mark - UIScrollViewDelegate
//将要开始拖拽时调用
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"scrollViewWillBeginDragging---");
//停止定时器
[self.timer setFireDate:[NSDate distantFuture]];
}
//松开拖拽时调用。如果松开时,scrollView还能惯性滚动,decelerate则是1。如果松开时,scrollView就停止滚动了,decelerate为0
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//间隔3s继续轮播
if (_autoPage) {
self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:ScrollInterval];
}
}
//调整循环显示的定位
- (void)adjustScrollLocation {
NSInteger page = self.collectionView.contentOffset.x/self.collectionView.bounds.size.width;
if (page == 0) {//滚动到最左边
self.collectionView.contentOffset = CGPointMake(self.collectionView.bounds.size.width * (self.dataArray.count - 2), 0);
self.pageControl.currentPage = self.dataArray.count-3;
}else if (page == self.dataArray.count - 1){//滚动到最右边
self.collectionView.contentOffset = CGPointMake(self.collectionView.bounds.size.width, 0);
self.pageControl.currentPage = 0;
}else{
self.pageControl.currentPage = page - 1;
}
}
#pragma mark - Setter
//设置数据时在第一个之前和最后一个之后分别插入数据
- (void)setData:(NSArray<NSString *> *)data {
self.dataArray = [NSMutableArray arrayWithArray:data];
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
if (data.count > 1) {
[self.dataArray addObject:data.firstObject];
[self.dataArray insertObject:data.lastObject atIndex:0];
[self.collectionView setContentOffset:CGPointMake(self.collectionView.bounds.size.width, 0)];
self.timer = [NSTimer scheduledTimerWithTimeInterval:ScrollInterval target:self selector:@selector(showNext) userInfo:nil repeats:true];
if(_autoPage == NO) {
self.timer.fireDate = [NSDate distantFuture];
}
}
self.pageControl.numberOfPages = data.count<=1 ? 0 : data.count;
self.pageControl.currentPage = 0;
[self.collectionView reloadData];
}
- (void)setAutoPage:(BOOL)autoPage {
_autoPage = autoPage;
NSDate *fireDate = autoPage ? [NSDate dateWithTimeIntervalSinceNow:ScrollInterval] : [NSDate distantFuture];
self.timer.fireDate = fireDate;
}
//自动显示下一个
- (void)showNext {
//手指拖拽是禁止自动轮播
if (self.collectionView.isDragging) {return;}
// CGFloat targetX = self.collectionView.contentOffset.x + self.collectionView.bounds.size.width;
// [self.collectionView setContentOffset:CGPointMake(targetX, 0) animated:true];
int index = self.collectionView.contentOffset.x/self.collectionView.frame.size.width;
[self.collectionView setContentOffset:CGPointMake((index+1)*self.collectionView.bounds.size.width, 0) animated:YES];
}
- (void)dealloc {
[self.timer invalidate];
self.timer = nil;
}
HRCollectionViewCell上面展示UIImageView,代码如下
@interface HRCollectionViewCell ()
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation HRCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self buildUI];
}
return self;
}
- (void)buildUI {
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:self.imageView];
}
-(void)setImageName:(NSString *)imageName{
UIImage *image = [UIImage imageNamed:imageName];
self.imageView.image = image;
}
@end
用UIScrollView实现轮播图的方式与HRCycleView代码很相似,具体实现和完整代码可查看:CycleCollectionView
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
MIMO技术的优缺点优点通过下面三个增益来总体概括:阵列增益。阵列增益是指由于接收机通过对接收信号的相干合并而活得的平均SNR的提高。在发射机不知道信道信息的情况下,MIMO系统可以获得的阵列增益与接收天线数成正比复用增益。在采用空间复用方案的MIMO系统中,可以获得复用增益,即信道容量成倍增加。信道容量的增加与min(Nt,Nr)成正比分集增益。在采用空间分集方案的MIMO系统中,可以获得分集增益,即可靠性性能的改善。分集增益用独立衰落支路数来描述,即分集指数。在使用了空时编码的MIMO系统中,由于接收天线或发射天线之间的间距较远,可认为它们各自的大尺度衰落是相互独立的,因此分布式MIMO
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
通常,数组被实现为内存块,集合被实现为HashMap,有序集合被实现为跳跃列表。在Ruby中也是如此吗?我正在尝试从性能和内存占用方面评估Ruby中不同容器的使用情况 最佳答案 数组是Ruby核心库的一部分。每个Ruby实现都有自己的数组实现。Ruby语言规范只规定了Ruby数组的行为,并没有规定任何特定的实现策略。它甚至没有指定任何会强制或至少建议特定实现策略的性能约束。然而,大多数Rubyist对数组的性能特征有一些期望,这会迫使不符合它们的实现变得默默无闻,因为实际上没有人会使用它:插入、前置或追加以及删除元素的最坏情况步骤复