美文网首页iOS开发
iOS 3D 轮播图,scrollView 自定义分页

iOS 3D 轮播图,scrollView 自定义分页

作者: BlackStar暗星 | 来源:发表于2020-06-10 15:11 被阅读0次

效果图


QQ20200603-160552-HD.gif QQ20200603-161052-HD.gif

主要知识点:

  • 无限轮播原理
  • 自动轮播 timer 导致页面无法释放问题
  • collectionView 自定义分页大小及UICollectionViewFlowLayout其他问题

1、无限轮播原理

轮播原始数据为 NSArray *dataArr = @[1,2,3] ,构造新数据,添加三次数据源

[self.newDataArr removeAllObjects];
[self.newDataArr addObjectsFromArray:dataArr];
[self.newDataArr addObjectsFromArray:dataArr];
[self.newDataArr addObjectsFromArray:dataArr];

新数据为 self.NewDataArr = @[1,2,3,1,2,3,1,2,3]

新数据构造完成后,将collectionView的初始位置更改为中间的数据源起始位置,也就是第二个1的位置(下标为3)

self.currentPageIndex = dataArr.count;

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath 
indexPathForRow:self.currentPageIndex inSection:0] 
atScrollPosition:UICollectionViewScrollPositionNone animated:NO];

这样就确保了scrollVeiw的 两边都是有数据的,我们要做的就是利用setContentOffset方法,让 collectionView 始终展示中间的数据源即可
示例代码

NSInteger newPageIndex = self.currentPageIndex;
            
if (self.currentPageIndex < self.dataArr.count) {
                
    newPageIndex = self.dataArr.count + self.currentPageIndex;
                
}else if (self.currentPageIndex >= self.dataArr.count*2){
                
    newPageIndex = self.currentPageIndex - self.dataArr.count;
}

计算了当前页面的pageIndex后,通过 pageIndex 计算实际偏移量,然后 setContentOffset

※※※
为什么这里要用 setContentOffset ,而不用 scrollToItemAtIndexPath 呢,因为我在使用 scrollToItemAtIndexPath 发现如果 使用3d效果,就会出现UI错乱的问题,使用3D效果+ scrollToItemAtIndexPath,打印出来的偏移量发现并不正确,具体原因没找到

2、自动轮播 timer 导致页面无法释放问题

如果 调用 scheduledTimerWithTimeInterval 方法执行timer时 ,repeats:YES的话,timer就会强持self,导致self无法释放。一般情况下我们只要在需要的时候,调用

[self.timer invalidate];
self.timer = nil;

就可以停止timer,然后释放 self

但是很多时候,我们无法在确定的某个点去做 timer 的置空,所以我们需要使用其他方法,让 timer 不在强持 self 。

使用NSObject 类提供的方法,实现消息转发可以解决此问题

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel 

- (void)forwardInvocation:(NSInvocation *)invocation

具体做法:自定义一个 中间类,继承NSObject,然后引入 weak 声明需要发送消息的对象

中间类 TimerTarget 如下:

@interface TimerTarget : NSObject

@property (nonatomic ,weak) BSLooperView * target;

@end
#pragma mark -
@implementation TimerTarget

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
    return [self.target methodSignatureForSelector:sel];
}

- (void)forwardInvocation:(NSInvocation *)invocation {
    [invocation invokeWithTarget:self.target];
}
@end

我们在初始化timer的时候 ,将self 作为 TimerTarget 的target,然后将TimerTarget 作为timer的target 即可

self.timerTarget = [[TimerTarget alloc]init];
self.timerTarget.target = self;

self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration
 target:self.timerTarget selector:@selector(looperTime) userInfo:nil 
repeats:YES];

3、collectionView 自定义分页大小及UICollectionViewFlowLayout其他问题

  • 自定义分页

自定义 UICollectionViewFlowLayout,重写 prepareLayout 方法 ,利用消息转发,重新设置页面间距,页面坐标

/// collectionView 的 宽度 去掉 item 宽度
CGFloat contentInset = self.collectionView.width - self.itemSize.width;        
/// 减速模式
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
/// 算出内边距
self.collectionView.contentInset = UIEdgeInsetsMake(0, contentInset*0.5, 0, contentInset*0.5);
/// 设置每个页面之间的间距
if ([self.collectionView respondsToSelector:NSSelectorFromString(@"_setInterpageSpacing:")]) {
   ((void(*)(id,SEL,CGSize))objc_msgSend)(self.collectionView,NSSelectorFromString(@"_setInterpageSpacing:"),CGSizeMake(-(contentInset-self.minimumLineSpacing), 0));
}
/// 设置 page 的坐标(原理:正常坐标是 0.0,如果想让左边留下 30 的宽度,就需要 page 左移 30)
/// 左移如果想要 item 的边距均分,就需要 左移 contentInset*0.5
if ([self.collectionView respondsToSelector:NSSelectorFromString(@"_setPagingOrigin:")]) {
   ((void(*)(id,SEL,CGPoint))objc_msgSend)(self.collectionView,NSSelectorFromString(@"_setPagingOrigin:"),CGPointMake(-contentInset*0.5, 0));
}
  • layoutAttributesForElementsInRect 方法
    我们在重写 layoutAttributesForElementsInRect 时
NSArray *originArr = [super layoutAttributesForElementsInRect:rect];

不能直接修改originArr数组内容,需要将他copy后再返回

NSArray *originArr = [super layoutAttributesForElementsInRect:rect];
NSArray * array = [[NSArray alloc]initWithArray:originArr copyItems:YES];
for (UICollectionViewLayoutAttributes * attrs in array) {
   /// 修改
}

如果你的布局需要动态更新,则需要调用此方法

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
   return YES;
}

demo 下载地址

相关文章

网友评论

    本文标题:iOS 3D 轮播图,scrollView 自定义分页

    本文链接:https://www.haomeiwen.com/subject/tlrpzhtx.html