iOS 用户引导 镂空 虚线

写在前面

在开发的过程中,偶尔会遇到需要在引导图上进行 镂空、画虚线,找了一下资料,写了下工具类,具体实现的效果如下。

Paste_Image.png

ZZHollowUtil.h文件

#import <Foundation/Foundation.h>

/**
 这个类用来镂空UIView,并且在镂空部分边缘添加虚线
 注意: 一个UIView 只能调用一次 -addHollowWithPath:inView: ,否则会有冲突,显示效果上会有问题
 建议: 将需要镂空的路径,用UIBezierPath 的 -appendPath: 方法加在一起,再调用。
 -addLineWithCGPath:inView: 可以多次调用
 
 例如:
 
 UIBezierPath *path = [UIBezierPath bezierPath];
 [path appendPath:path_0];
 [path appendPath:path_1];
 
 
 [ZZHollowUtil addHollowWithPath:path inView:self];
 [ZZHollowUtil addLineWithCGPath:path_1.CGPath inView:self];
 [ZZHollowUtil addLineWithCGPath:path_2.CGPath inView:self];
 
 */
@interface ZZHollowUtil : NSObject

// 镂空
+ (void)addHollowWithPath:(UIBezierPath *)path inView:(UIView *)view;

// 画虚线 默认:黑白相间
+ (void)addLineWithCGPath:(CGPathRef)path inView:(UIView *)view;

+ (void)addLineWithCGPath:(CGPathRef)path
             stockerColor:(UIColor *)stokerColor
                fillColor:(UIColor *)fillColor
                   inView:(UIView *)view;
@end

ZZHollowUtil.m文件

#import "ZZHollowUtil.h"

@implementation ZZHollowUtil

+ (void)addHollowWithPath:(UIBezierPath *)path inView:(UIView *)view {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        UIBezierPath *aPath = [UIBezierPath bezierPathWithRect:view.bounds];
        [aPath appendPath:[path bezierPathByReversingPath]];
        
        //创建出CAShapeLayer
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        
        //让贝塞尔曲线与CAShapeLayer产生联系
        shapeLayer.path = aPath.CGPath;
        
        //添加并显示
        [view.layer setMask:shapeLayer];
    });
}

+ (void)addLineWithCGPath:(CGPathRef)path inView:(UIView *)view {
    
    [self addLineWithCGPath:path
               stockerColor:[UIColor whiteColor]
                  fillColor:[UIColor blackColor]
                     inView:view];
}

+ (void)addLineWithCGPath:(CGPathRef)path
             stockerColor:(UIColor *)stokerColor
                fillColor:(UIColor *)fillColor
                   inView:(UIView *)view
{
    CAShapeLayer *border = [CAShapeLayer layer];
    
    border.strokeColor = stokerColor.CGColor;
    
    border.fillColor = fillColor.CGColor;
    
    border.path = path;
    
    border.frame = view.bounds;
    
    border.lineWidth = 2.f;
    
    border.lineCap = @"square";
    
    border.lineDashPattern = @[@6, @4];
    
    [view.layer addSublayer:border];
}

@end

如果有遇到问题,在下边评论,第一时间回复。
如果有更好的实现方式,欢迎讨论。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,049评论 25 709
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,677评论 9 468
  • 前两天,看见简书上有一个活动:“写一篇读书感悟,送你一本书,我们一共准备了160本书”, 内容主要是:关注公众号简...
    我是骆驼草阅读 583评论 2 6
  • 尹泡泡是一个可爱的女孩,但是因为一次意外,失去了记忆,被一户人家收养。 名为甜心,泡泡百思不得其解,为什么失去了那...
    冷血玫瑰阅读 252评论 0 0
  • 倘若诗人们都睡了 月儿该找谁聊天 冬猫吗? 它蜷缩成了肉球 老鼠爱扑腾,很吵 狗?最好别 它哈巴的永远是主人 不找...
    封城雪阅读 201评论 9 6