zoukankan      html  css  js  c++  java
  • UICollectionView自定义布局( 封面布局)

    UICollectionView自定义布局 封面布局


    布局文件:

    MKCoverFlowLayout.h

    #import <UIKit/UIKit.h>
    @interface MKCoverFlowLayout : UICollectionViewFlowLayout
    @end
    

    MKCoverFlowLayout.m

    #import "MKCoverFlowLayout.h"
    @implementation MKCoverFlowLayout
    #define ZOOM_FACTOR 0.35
    
    -(void) prepareLayout {
    
    //水平滚动
      self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
      CGSize size = self.collectionView.frame.size;
      self.itemSize = CGSizeMake(size.width/4.0f, size.height*0.7);
      self.sectionInset = UIEdgeInsetsMake(size.height * 0.15, size.height * 0.1, size.height * 0.15, size.height * 0.1);
    }
    
    - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds
    {
    	return YES;
    }
    
    -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
    {
    NSArray* array = [super layoutAttributesForElementsInRect:rect];
    CGRect visibleRect;
    visibleRect.origin = self.collectionView.contentOffset;
    visibleRect.size = self.collectionView.bounds.size;
    float collectionViewHalfFrame = self.collectionView.frame.size.width/2.0f;
    for (UICollectionViewLayoutAttributes* attributes in array) {
    
      if (CGRectIntersectsRect(attributes.frame, rect)) {
          
      CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
      CGFloat normalizedDistance = distance / collectionViewHalfFrame;
      if (ABS(distance) < collectionViewHalfFrame) {
        CGFloat zoom = 1 + ZOOM_FACTOR*(1 - ABS(normalizedDistance));
        
        //3D变化
        //旋转的角度随着离中心的距离而按比例增加
          //如果物件在中间,其旋转角度为0
          //角度随着离中心的距离二而比例增加
        CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
        rotationAndPerspectiveTransform.m34 = 1.0 / -500;
        rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, (normalizedDistance) * M_PI_4, 0.0f, 1.0f, 0.0f);
        CATransform3D zoomTransform = CATransform3DMakeScale(zoom, zoom, 1.0);
        attributes.transform3D = CATransform3DConcat(zoomTransform, rotationAndPerspectiveTransform);
          
        attributes.zIndex = ABS(normalizedDistance) * 10.0f;
        CGFloat alpha = (1 - ABS(normalizedDistance)) + 0.1;
        if(alpha > 1.0f) alpha = 1.0f;
        attributes.alpha = alpha;
      } else {
        
        attributes.alpha = 0.0f;
      }
    }
    }
    return array;
    }
    
    
    @end
  • 相关阅读:
    远程网络时间同步在分布式测控与实时仿真系统应用
    GPS对时装置(北斗卫星同步时钟)应用市场调研分析
    时间同步服务器(NTP时钟同步服务器)如何正确的选购?
    NTP授时服务器(卫星同步时钟)与物联网十大应用
    App 自动化环境搭建(基于 Appium)
    let var作用域
    vue methods和computed效率比较
    vue computed
    vue computed
    vue v-bind:style
  • 原文地址:https://www.cnblogs.com/sunyanyan/p/5283144.html
Copyright © 2011-2022 走看看