首页 技术 正文
技术 2022年11月15日
0 收藏 497 点赞 4,453 浏览 2455 个字

1. 自定义UICollectionLayout —- 正三角形居中布局自定义 —UICollectionViewLayout-正N变形居中布局

支持多个图形的自动布局

自定义 —UICollectionViewLayout-正N变形居中布局

2. 自定义UICollectionLayout —- 正方形居中布局

自定义 —UICollectionViewLayout-正N变形居中布局

滚动展示的区域

自定义 —UICollectionViewLayout-正N变形居中布局

3. 自定义UICollectionLayout —- 正六边形居中布局(蜂窝布局)

自定义 —UICollectionViewLayout-正N变形居中布局

等等正n变形布局,其中 正六边形支持 间距大小的改变,图形的旋转。

自定义 —UICollectionViewLayout-正N变形居中布局

旋转 —

自定义 —UICollectionViewLayout-正N变形居中布局

间距

自定义 —UICollectionViewLayout-正N变形居中布局

4. 核心代码 —- 绘制正n边形 (贝塞尔曲线)

- (void)layoutSubviews{    [super layoutSubviews];    // step 1: 绘制正n变形    CGFloat X = self.contentView.frame.size.width  * 0.5;    CGFloat Y = self.contentView.frame.size.height * 0.5;    UIBezierPath* bezierPath = [UIBezierPath bezierPath];    bezierPath.lineCapStyle = kCGLineCapRound;    bezierPath.lineJoinStyle = kCGLineCapRound;    CGFloat angle = labs(360/self.number);    [bezierPath moveToPoint: CGPointMake(X - mm , Y)];    for (int i = 0; i < self.number - 1; i ++) {        CGFloat angle1 = angle *(i + 1);        CGFloat XX = X + (mm * (COS(angle1)))*(-1);        CGFloat YY = Y + mm * (SIN(angle1));        [bezierPath addLineToPoint: CGPointMake(XX, YY)];    }    [bezierPath addLineToPoint: CGPointMake(X - mm , Y)];    // step 2: 根据路径生成蒙板    CAShapeLayer *maskLayer = [CAShapeLayer layer];    maskLayer.path = [bezierPath CGPath];    self.backgroundColor = [UIColor redColor];    // step 3: 给cell添加模版    self.layer.mask = maskLayer;    self.titleLabel.frame = self.bounds;}

5. 核心代码 —  自定义Layout布局

- (void)prepareLayout{    [super prepareLayout];    _itemCount = [self.collectionView numberOfItemsInSection:];    _attributeAttay = [[NSMutableArray alloc]init];     ; i < _itemCount; i ++) {        //设置第一个正六边形的中心位置        CGFloat X = self.collectionView.frame.size.width*0.5;        CGFloat Y = self.TopMargin;         == ? -:i%;         == ? :i%;        X += CST(-num)*([self judgeXWithNumber:self.number]);        Y += ([self judgeSizeWithNumber:self.number].height + self.margin)*(i/) + CST(num2)*([self judgeSizeWithNumber:self.number].height/)+self.margin;        UICollectionViewLayoutAttributes *attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForRow:i inSection:]];        // 设置每隔item的大小        attribute.size = [self judgeSizeWithNumber:self.number];        // 设置每隔item的圆心        // 计算每个item的中心坐标        attribute.center = CGPointMake(X, Y);        [_attributeAttay addObject:attribute];    }}// X轴方向的间隔- (CGFloat)judgeXWithNumber:(NSInteger)xNumber{     ) {        /xNumber)*mm;    }    )    {        /xNumber)*mm;    }    )    {        /xNumber)*mm - ;    }    )    {        /xNumber)*mm;    }    )    {        /xNumber)*mm;    }    else    {        *mm + self.margin ;    }}// 就算正n变形的大小- (CGSize)judgeSizeWithNumber:(NSInteger)sNumber{     || sNumber ==  ) {        *mm,*SIN(/sNumber)*mm);    }    else    {        *(mm),*mm);    }}//设置内容区域的大小-(CGSize)collectionViewContentSize{    CGFloat hh = [self judgeSizeWithNumber:self.number].height;    CGFloat height = (hh+self.margin)*(self.itemCount/ + ) + self.TopMargin + ;    if (height > Device_height) {        height = height;    }    else    {        height = Device_height;    }    return CGSizeMake(Device_width, height);}//返回设置数组-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{    return _attributeAttay;}

View Layout

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,355
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,138
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848