UIColletionView

自定义flowlayout实现分页

collectionView 分页 & 偏移位置

self.previousOffsetX = 0;
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
  // 分页以1/3处
  if (proposedContentOffset.x > self.previousOffsetX + self.itemSize.width / 3.0) {
    self.previousOffsetX += self.collectionView.frame.size.width - self.minimumLineSpacing * 2;
  } else if (proposedContentOffset.x < self.previousOffsetX  - self.itemSize.width / 3.0) {
    self.previousOffsetX -= self.collectionView.frame.size.width - self.minimumLineSpacing * 2;
  }
  
  proposedContentOffset.x = self.previousOffsetX;
  
  return proposedContentOffset;

collectionView item 缩放

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
  NSArray *superAttributes = [super layoutAttributesForElementsInRect:rect];
  NSArray *attributes = [[NSArray alloc] initWithArray:superAttributes copyItems:YES];
  
  CGRect visibleRect = CGRectMake(self.collectionView.contentOffset.x,
                                  self.collectionView.contentOffset.y,
                                  self.collectionView.frame.size.width,
                                  self.collectionView.frame.size.height);
  CGFloat offset = CGRectGetMidX(visibleRect);
  
  [attributes enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes *attribute, NSUInteger idx, BOOL * _Nonnull stop) {
    CGFloat distance = offset - attribute.center.x;
    // 越往中心移动,值越小,那么缩放就越小,从而显示就越大
    // 同样,超过中心后,越往左、右走,缩放就越大,显示就越小
    CGFloat scaleForDistance = distance / self.itemSize.height;
    // 0.2可调整,值越大,显示就越大
    CGFloat scaleForCell = 1 + 0.2 * (1 - fabs(scaleForDistance));
    
    // only scale y-axis
    attribute.transform3D = CATransform3DMakeScale(1, scaleForCell, 1);
    attribute.zIndex = 1;
  }];
  
  return superAttributes;
}
- (void)prepareLayout {
  self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  self.minimumLineSpacing = 20;
  //  self.minimumInteritemSpacing = 20;
  self.sectionInset = UIEdgeInsetsMake(0, 30, 0, 30);
  self.itemSize = CGSizeMake(self.collectionView.frame.size.width - 60,
                             self.collectionView.frame.size.height - 180);
  [super prepareLayout];
}

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

瀑布流

http://www.jianshu.com/p/826a43d28a39

卡片吸顶布局

http://www.cocoachina.com/ios/20161212/18253.html

你可能感兴趣的:(UIColletionView)