前言
由于開(kāi)發(fā)中遇到一個(gè)既有按鈕可以點(diǎn)擊展示上一個(gè)和下一個(gè),也可以手動(dòng)滑動(dòng)展示上一個(gè)下一個(gè),導(dǎo)致監(jiān)聽(tīng)UICollectionView到底展示的是哪個(gè)indexPath比較麻煩,特記錄一下
參考:
- UITableView、UICollectionView 滾動(dòng)結(jié)束的監(jiān)測(cè)(類(lèi)似ViewDidAppear)
- 如何獲取頂部CollectionView當(dāng)前顯示的cell的indexPath
監(jiān)聽(tīng)滾動(dòng)停止:
注意:頁(yè)面中點(diǎn)擊上一個(gè)、下一個(gè)按鈕調(diào)用的
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;不會(huì)走這個(gè)滾動(dòng)的回調(diào)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 停止類(lèi)型1、停止類(lèi)型2
BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (scrollToScrollStop) {
[self scrollViewDidEndScroll];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
// 停止類(lèi)型3
BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (dragToDragStop) {
[self scrollViewDidEndScroll];
}
}
}
#pragma mark - scrollView 停止?jié)L動(dòng)監(jiān)測(cè)
- (void)scrollViewDidEndScroll {
NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *indexPath = indexPaths.firstObject;
// 將collectionView在控制器view的中心點(diǎn)轉(zhuǎn)化成collectionView上的坐標(biāo)
// CGPoint pInView = [self convertPoint:self.collectionView.center toView:self.collectionView];
// // 獲取這一點(diǎn)的indexPath
// NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pInView];
}
調(diào)用UICollectionView的reloadData后立刻調(diào)用scrollToItemAtIndexPath...,則scrollToItemAtIndex不會(huì)起作用,需要在relaodData后延遲一下調(diào)用
- (void)awakeFromNib {
[super awakeFromNib];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.collectionView registerNib:[UINib nibWithNibName:@"xxxxCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"xxxxCollectionViewCell"];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.scrollEnabled = YES;
self.collectionView.pagingEnabled = YES;
self.collectionView.showsHorizontalScrollIndicator = NO;
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataList.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(Main_Screen_Width, collectionView.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
xxxxCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"xxxxCollectionViewCell" forIndexPath:indexPath];
XXXModel *model = self.dataList[indexPath.item];
cell.model = model;
return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 停止類(lèi)型1、停止類(lèi)型2
BOOL scrollToScrollStop = !scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (scrollToScrollStop) {
[self scrollViewDidEndScroll];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
// 停止類(lèi)型3
BOOL dragToDragStop = scrollView.tracking && !scrollView.dragging && !scrollView.decelerating;
if (dragToDragStop) {
[self scrollViewDidEndScroll];
}
}
}
#pragma mark - scrollView 停止?jié)L動(dòng)監(jiān)測(cè)
- (void)scrollViewDidEndScroll {
NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *indexPath = indexPaths.firstObject;
// 將collectionView在控制器view的中心點(diǎn)轉(zhuǎn)化成collectionView上的坐標(biāo)
// CGPoint pInView = [self convertPoint:self.collectionView.center toView:self.collectionView];
// // 獲取這一點(diǎn)的indexPath
// NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pInView];
self.selectIndex = indexPath.item;
[self updateShowTitle];
}
- (IBAction)preBtnClicked:(id)sender {
self.selectIndex -=1;
[self updateShow];
}
- (IBAction)nextBtnClicked:(id)sender {
self.selectIndex +=1;
[self updateShow];
}
- (void)setDataList:(NSArray<VIPLevelDataModel *> *)dataList
{
_dataList = dataList;
[self initUpdateShow];
}
- (void)updateShowTitle
{
if(self.dataList.count == 0){
return;
}
if(self.selectIndex == 0){
self.preBtn.enabled = NO;
}else{
self.preBtn.enabled = YES;
}
if(self.selectIndex >= self.dataList.count-1){
self.nextBtn.enabled = NO;
}else{
self.nextBtn.enabled = YES;
}
NSString *imageName = [NSString stringWithFormat:@"vip_red%ld",self.selectIndex];
self.vipImage.image = [UIImage imageNamed:imageName];
}
- (void)initUpdateShow
{
if(self.dataList.count == 0){
return;
}
[self updateShowTitle];
[self.collectionView reloadData];
WS(weakSelf)
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self
.selectIndex inSection:0];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
});
}
- (void)updateShow
{
if( self.dataList.count == 0){
return;
}
[self updateShowTitle];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectIndex inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}