
正文
UIScollview 添加UICollectionView 实现放大缩小
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
- 创建一个空的工程
-
打开storyboard,添加UIScollview
- 设置代理
-
实现代理方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return self.mCollectionView; } - 设置放大缩小的倍数
-
-
添加UICollectionView
- 设置代理
- 设置reuseIdentifier 为cell
-
实现代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ ; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identify = @"cell"; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath]; [cell sizeToFit]; return cell; } #pragma mark -- UICollectionViewDelegate //设置每个 UICollectionView 的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { -, ); } //定义每个UICollectionView 的间距 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ , , ,); } //定义每个UICollectionView 的纵向间距 -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ ; }
- 最重要的在 - (void)viewDidLoad加入下面两行代码
-
UIGestureRecognizer *gesture = self.mCollectionView.pinchGestureRecognizer; [self.mCollectionView removeGestureRecognizer:gesture];
去掉这个手势 不然会导致崩溃 报错 :The view returned from viewForZoomingInScrollView: must be a subview of the scroll view. It can not be the scroll view itself.'
-
最终效果图
-
- 项目地址:https://github.com/wandou911/ScrollCollectionView/
- 录屏gif小工具:http://www.pc6.com/mac/135257.html







