- 根據(jù)偏移量offset每次滾動特定的距離(類似pagingEnabled屬性)
圖片:
@interface N2_HomeWordsStudyHeaderView ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) CGFloat myOffsetX; // 滑動時的偏移量
@end
@implementation N2_HomeWordsStudyHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self setupUI];
}
return self;
}
- (void)setupUI {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.pagingEnabled = NO; /// 禁止分頁滑動時科汗,根據(jù)偏移量判斷滑動到第幾個item
collectionView.scrollEnabled = YES; /// 禁止滑動
collectionView.showsHorizontalScrollIndicator = NO;
[self addSubview:collectionView];
[collectionView makeConstraints:^(MASConstraintMaker *make) {
// make.left.mas_equalTo(self.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*20);
// make.right.top.bottom.equalTo(self);
make.edges.equalTo(self);
}];
[collectionView registerClass:[N2_homeHeaderWordBookStudyCell class] forCellWithReuseIdentifier:NSStringFromClass([N2_homeHeaderWordBookStudyCell class])];
[collectionView registerClass:[N2_homeHeaderWordBookStudiedCell class] forCellWithReuseIdentifier:NSStringFromClass([N2_homeHeaderWordBookStudiedCell class])];
self.collectionView = collectionView;
self.collectionView.backgroundColor = [UIColor whiteColor];
// [self addLeftAndRightTouchGesture];
}
- (void)refreshHomeNewWordStudyCollectionView {
[self.collectionView reloadData];
}
#pragma mark -- <UICollectionViewDelegate, UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.planDetailModelLists.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// item 大小
return CGSizeMake([UIScreen mainScreen].bounds.size.width/375*335, [UIScreen mainScreen].bounds.size.width/375*525);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
// 行間距(上下)
// return 40;
return [UIScreen mainScreen].bounds.size.width/375*10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
// 列間距(左右)
return [UIScreen mainScreen].bounds.size.width/375*1;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake([UIScreen mainScreen].bounds.size.width/375*5, [UIScreen mainScreen].bounds.size.width/375*20, [UIScreen mainScreen].bounds.size.width/375*5, [UIScreen mainScreen].bounds.size.width/375*20);
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
N2_homeHeaderWordBookStudiedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([N2_homeHeaderWordBookStudiedCell class]) forIndexPath:indexPath];
if (self.planDetailModelLists.count > 0) {
cell.planDetailModel = self.planDetailModelLists[indexPath.row];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
N2_homeHeaderWordBookStudiedCell *cell = (N2_homeHeaderWordBookStudiedCell *)[collectionView cellForItemAtIndexPath:indexPath];
}
#pragma mark -- -- 以下四個方法喷面,就是實現(xiàn)UICollectionView根據(jù)偏移量offset滑動零酪,實現(xiàn)(pagingEnabled)效果的方法
/*
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"開始滑動 scrollView.contentOffset.x = %.2f ", scrollView.contentOffset.x);
// 整個引導頁先弄一個UIViewController互广,在VC中添加其他需要的東西报亩,比如一個UICollectionView宋梧、“跳過”按鈕虐译,和翻頁控件UIpageControl
// 可以設置剛開始進入APP時的引導圖,可以使用UICollectionView鹅龄,再在上面添加“跳過”按鈕
// 原理:當scrollView.contentOffset.x < 0時揩慕,禁止向右滑動,同理扮休,滑到最后一個item迎卤,也設置禁止在向左滑動
if (scrollView.contentOffset.x < 0) {
self.collectionView.scrollEnabled = NO;
} else {
self.collectionView.scrollEnabled = YES;
}
}
**/
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
// 停止滑動時,當前的偏移量(即最近停止的位置)
self.myOffsetX = scrollView.contentOffset.x;
NSLog(@"scrollView.contentOffset.x = %.2f", self.myOffsetX);
}
/// collectionView.pagingEnabled = NO; /// 禁止分頁滑動時玷坠,根據(jù)偏移量判斷滑動到第幾個item
/// 滑動 “減速滾動時” 是觸發(fā)的代理蜗搔,當用戶用力滑動或者清掃時觸發(fā)
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
// 為了簡化代碼用了一個手勢從滑動方向判斷,所以用到了絕對值比對判斷 左右滑動的值八堡。
// fabs :處理double類型的取絕對值
if (fabs(scrollView.contentOffset.x - self.myOffsetX) > 10) {
[self scrollToNextPageOrLastPage:scrollView];
}
}
/// 用戶拖拽時 調(diào)用
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if (fabs(scrollView.contentOffset.x - self.myOffsetX) > 10) {
[self scrollToNextPageOrLastPage:scrollView];
}
}
- (void)scrollToNextPageOrLastPage:(UIScrollView *)scrollView {
/// 之前停止的位置樟凄,判斷左滑、右滑
if (scrollView.contentOffset.x > self.myOffsetX) { // i > 0(左滑兄渺,下一個(i最大為cell個數(shù)))
// 計算移動的item的個數(shù)(item.width + 間距)
NSInteger i = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width/375*(335+10)+1;
NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
// item居中顯示
[self.collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
NSLog(@"??index = %ld", i);
}else{ // i <= 0 (右滑缝龄,上一個)(i 最小為-1,所以有需要的話挂谍,在這里添加判斷叔壤,使其最小為 i = 0)
NSInteger i = scrollView.contentOffset.x/[UIScreen mainScreen].bounds.size.width/375*(335+10)+1;
NSIndexPath *index = [NSIndexPath indexPathForRow:i-1 inSection:0];
[self.collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
NSLog(@"??????index = %ld", i-1);
}
}
@end