# LBMiddleExpandLayout **Repository Path**: romancao/lbmiddle-expand-layout ## Basic Information - **Project Name**: LBMiddleExpandLayout - **Description**: 一个iOS中间放大自定义layout - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-10-25 - **Last Updated**: 2024-10-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # LiuboMiddleExpandLayout [![CI Status](https://img.shields.io/travis/liubo/LiuboMiddleExpandLayout.svg?style=flat)](https://travis-ci.org/liubo/LiuboMiddleExpandLayout) [![Version](https://img.shields.io/cocoapods/v/LiuboMiddleExpandLayout.svg?style=flat)](https://cocoapods.org/pods/LiuboMiddleExpandLayout) [![License](https://img.shields.io/cocoapods/l/LiuboMiddleExpandLayout.svg?style=flat)](https://cocoapods.org/pods/LiuboMiddleExpandLayout) [![Platform](https://img.shields.io/cocoapods/p/LiuboMiddleExpandLayout.svg?style=flat)](https://cocoapods.org/pods/LiuboMiddleExpandLayout) ## Example To run the example project, clone the repo, and run `pod install` from the Example directory first. ## Requirements ## Installation LiuboMiddleExpandLayout is available through [CocoaPods](https://cocoapods.org). To install it, simply add the following line to your Podfile: ```ruby pod 'LiuboMiddleExpandLayout' - (UICollectionView *)collectionView { if (!_collectionView) { LBMiddleExpandLayout *layout = [[LBMiddleExpandLayout alloc] init]; layout.itemSize = CGSizeMake(284 * PLUS_SCALE, 582*PLUS_SCALE); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.minimumLineSpacing = 28*PLUS_SCALE; layout.minimumInteritemSpacing = 28*PLUS_SCALE; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.pagingEnabled = NO; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.backgroundColor = [UIColor clearColor]; [_collectionView registerClass:[LBCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([LBCollectionViewCell class])]; [_collectionView setFrame:CGRectMake(0, 9 * PLUS_SCALE, SCREEN_WIDTH,SCREEN_HEIGHT - 9 * PLUS_SCALE - 90 * PLUS_SCALE)]; } return _collectionView; } //手指拖动开始 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { self.m_dragStartX = scrollView.contentOffset.x; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (self.endDraggingOffset < 0 && scrollView.contentOffset.x >= self.endDraggingOffset) { //这里是为了上刷新的时候,停留在固定的偏移量,展示loading scrollView.contentOffset = CGPointMake(self.endDraggingOffset, 0); } if (!self.hasNextPage) { return; } /* 遇到一次精度问题,正在刷新的时候 应该展示loading,但是由于精度问题,这时候 scrollView.contentOffset.x = -52.333333 , refreshThreshold = -52.399998, 如果 是 (scrollView.contentOffset.x <= refreshThreshold ),就会造成loading隐藏, 所以这里+0.5 */ if (scrollView.contentOffset.x <= (refreshThreshold + 0.5)) { self.refreshView.hidden = NO; } else { self.refreshView.hidden = YES; } } //手指拖动停止 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { self.m_dragEndX = scrollView.contentOffset.x; dispatch_async(dispatch_get_main_queue(), ^{ [self fixCellToCenter]; }); if (scrollView.contentOffset.x < refreshThreshold) { if (!self.hasNextPage) { return; } if (self.isLoadingMore) { return; } self.endDraggingOffset = refreshThreshold; NSMutableArray *array = [NSMutableArray array]; for (int i = (self.signsArray.count + 10); i > self.signsArray.count; i --) { [array addObject:[NSString stringWithFormat:@"%d", i]]; } [self refreshHandleWithArray:array]; self.isLoadingMore = YES; } } #pragma mark - private - (void)fixCellToCenter { //最小滚动距离 float dragMiniDistance = self.view.bounds.size.width/20.0f; if (self.m_dragStartX - self.m_dragEndX >= dragMiniDistance) { self.m_currentIndex -= 1;//向右 } else if(self.m_dragEndX - self.m_dragStartX >= dragMiniDistance){ self.m_currentIndex += 1;//向左 } NSInteger maxIndex = [_collectionView numberOfItemsInSection:0] - 1; self.m_currentIndex = self.m_currentIndex <= 0 ? 0 : self.m_currentIndex; self.m_currentIndex = self.m_currentIndex >= maxIndex ? maxIndex : self.m_currentIndex; [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.m_currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; } ``` ## Demonstrate ![Image text](https://gitee.com/liuboliu/lbmiddle-expand-layout/raw/main/2023-07-08%2019.52.38.gif) ![Image text](https://gitee.com/liuboliu/lbmiddle-expand-layout/raw/main/2024-06-02%2018.26.56.gif) ## Author liubo, liubo@thepaper.cn ## License LiuboMiddleExpandLayout is available under the MIT license. See the LICENSE file for more info.