美文网首页iOS常用
iOS开发多边形(六边形和八边形)和处理点击事件

iOS开发多边形(六边形和八边形)和处理点击事件

作者: chasitu | 来源:发表于2020-09-07 09:24 被阅读0次

有一段时间没有写简书了,进来乍一看有种跑错场的感觉,都是各种抒情的文章,技术文章少了很多,我也有一种想去别的平台的冲动,但是还是在这里继续更新吧


效果图
用贝塞尔曲线画多边形

我们平时使用多边形的时候用列表比较多,所以今天使用UIcollectionViewCell,其它正常使用UIcollectionView的相关代码我就不多做解释了啊,就说重点部分,我们直接上代码

SHCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0];
    UIBezierPath *path = [UIBezierPath bezierPath];
    if (indexPath.item>5) {//六边形
        CGFloat halfedgelength = cell.frame.size.width*0.25;
        [path moveToPoint:CGPointMake(cell.frame.size.width*0.5, 0)];
        [path addLineToPoint:CGPointMake(cell.frame.size.width, halfedgelength)];
        [path addLineToPoint:CGPointMake(cell.frame.size.width, halfedgelength*3)];
        [path addLineToPoint:CGPointMake(cell.frame.size.width*0.5, cell.frame.size.height)];
        [path addLineToPoint:CGPointMake(0, halfedgelength*3)];
        [path addLineToPoint:CGPointMake(0, halfedgelength)];
        [path closePath];
    }else{  //八边形
        CGFloat rate = sqrtf(8.0)/(2+2+sqrtf(8.0));
        CGFloat edgeLength = cell.frame.size.width*rate;
        [path moveToPoint:CGPointMake((cell.frame.size.width - edgeLength)*0.5, 0)];
        [path addLineToPoint:CGPointMake((cell.frame.size.width - edgeLength)*0.5+edgeLength, 0)];
        [path addLineToPoint:CGPointMake(cell.frame.size.width, (cell.frame.size.height-edgeLength)*0.5)];
        [path addLineToPoint:CGPointMake(cell.frame.size.width, (cell.frame.size.height - edgeLength)*0.5+edgeLength)];
        [path addLineToPoint:CGPointMake((cell.frame.size.width - edgeLength)*0.5+edgeLength, cell.frame.size.height)];
        [path addLineToPoint:CGPointMake((cell.frame.size.width - edgeLength)*0.5, cell.frame.size.height)];
        [path addLineToPoint:CGPointMake(0, (cell.frame.size.height - edgeLength)*0.5+edgeLength)];
        [path addLineToPoint:CGPointMake(0, (cell.frame.size.height - edgeLength)*0.5)];
        [path closePath];
    }
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = cell.bounds;
    maskLayer.path = path.CGPath;
    cell.layer.mask = maskLayer;
    cell.bezierPath = path;//保存UIBezierPath对象
    return cell;

注释

  • 主要是使用UIBezierPath曲线画的多边形,八边形坐标计算使用了《求平方根》函数,是math类的
  • 六边形和八边形的坐标系是默认正方形来计算的
  • 八边形比例中使用的数字不是唯一,只为获得相关比例
  • 底部UIBezierPath对象赋值是为了判断点击事件时使用
处理相关点击事件

SHCustomCollectionViewCell.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SHCustomCollectionViewCell : UICollectionViewCell
@property (nonatomic , strong) UIBezierPath *bezierPath;
@end

NS_ASSUME_NONNULL_END

SHCustomCollectionViewCell.m

#import "SHCustomCollectionViewCell.h"

@implementation SHCustomCollectionViewCell

- (void)setBezierPath:(UIBezierPath *)bezierPath
{
    _bezierPath = bezierPath;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return [self.bezierPath containsPoint:point] ? [super hitTest:point withEvent:event] : nil;
}
@end

注释:

  • 使用当前UIBezierPath对象判断点击位置是否在我们绘画的多边形内,超出范围返回nil,不做相关点击操作

这些代码小伙伴们可以直接复制过去就正常运行出来,有什么问题可以在评论区留言给我

相关文章

网友评论

    本文标题:iOS开发多边形(六边形和八边形)和处理点击事件

    本文链接:https://www.haomeiwen.com/subject/merkektx.html