刮一刮擦除效果

1.前言

之前需求有个刮一刮出答案的功能,做个记录,希望对需要的同学有帮助。

实现原理

给展示的view添加CAShapeLayer的mask,当我们滑动的时候,把轨迹添加到path上,添加的地方就可以显示出来。
不太懂mask的同学可以看ios CALayer之mask使用

2.实现方法

实现方法如下

2.1初始化
@interface LayerScrapeView()
@property (nonatomic, strong) UIImageView *scrapeMaskView;
@property (nonatomic, strong) UIView *scrapeContentView;
@property (nonatomic, strong) CAShapeLayer *maskLayer;
@property (nonatomic, strong) UIBezierPath *maskPath;
@end
- (instancetype)initWithFrame:(CGRect)frame
                  contentView:(UIView *)contentView
                     maskView:(UIImageView *)maskView{
    self = [super initWithFrame:frame];
    if(self){
        self.scrapeMaskView = maskView;
        [self addSubview:maskView];
        self.scrapeContentView = contentView;
        [self addSubview:contentView];
        
        self.maskLayer = [CAShapeLayer layer];
        self.maskLayer.strokeColor = [UIColor whiteColor].CGColor;
//        self.maskLayer.backgroundColor = [UIColor colorWithWhite:1 alpha:0].CGColor;
        self.maskLayer.lineWidth = 20;
        self.maskLayer.frame = contentView.bounds;
        self.maskLayer.lineCap = kCALineCapRound;
        self.scrapeContentView.layer.mask = self.maskLayer;
        self.maskPath = [UIBezierPath bezierPath];
    }
    return self;
}

2.2移动添加path

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:self.scrapeContentView];
    [self.maskPath addLineToPoint:point];
    [self.maskPath moveToPoint:point];
    self.maskLayer.path = self.maskPath.CGPath;
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIGraphicsBeginImageContextWithOptions(self.scrapeContentView.bounds.size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.scrapeContentView.layer renderInContext:context];
    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGFloat percent = 1 - [self pixelPercentWithImage:image];
    if(percent < 0.75) return ;//超过0.75则不用滑动
    [self showContentView];
    self.userInteractionEnabled = NO;
}

2.3计算透明占比

截图然后计算图片透明像素占比

- (CGFloat)pixelPercentWithImage:(UIImage *)image{
    NSInteger alphaCount = 0;
    CGImageRef imageRef = [image CGImage];
    size_t imageWidth = CGImageGetWidth(imageRef);
    size_t imageHeight = CGImageGetHeight(imageRef);
    size_t bytesPerRow = imageWidth * 4;
    uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast);
    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), imageRef);
    size_t pixelNum = imageWidth * imageHeight;
    uint32_t* pCurPtr = rgbImageBuf;
    for (int i = 0; i < pixelNum; i++, pCurPtr++){
        uint8_t *ptr = (uint8_t *)pCurPtr;
        if(ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0)alphaCount++; //透明颜色
    }
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(rgbImageBuf);
    return alphaCount*1.0/pixelNum;
}

3.补充

这里也有一种比较生硬的方式

-(void)touchesMoved:(NSSet<UITouch *>* )touches withEvent:(UIEvent* )event {
    UITouch * touch = touches.anyObject;
    CGPoint contentPoint = [touch locationInView:self.scrapeMaskView];
    CGRect rect = CGRectMake(contentPoint.x, contentPoint.y, 30, 30);
    
    UIGraphicsBeginImageContextWithOptions(self.scrapeMaskView.bounds.size, NO, [UIScreen mainScreen].scale);
    CGContextRef ref = UIGraphicsGetCurrentContext();
    [self.scrapeMaskView.layer renderInContext:ref];
    CGContextClearRect(ref, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.scrapeMaskView.image = image;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    UIImageWriteToSavedPhotosAlbum(self.scrapeMaskView.image, self, @selector(savedPhotoImage:savingWithError:contextInfo:), nil);
    CGFloat percent = [self pixelPercentWithImage:self.scrapeMaskView.image];
    NSLog(@"-----------%f",percent);
}

这种也可以实现,但是流畅度比较差,另外计算像素透明度会有很大的偏差。

4.demo

demo链接

参考资料

ios CALayer之mask使用
一个万能的刮刮乐控件

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,902评论 1 32
  • 参考文档:https://pan.baidu.com/s/1HaQRu8c8bNfKTSbxF5__Sw相同内容网...
    liboxiang阅读 3,342评论 0 1
  • 书写的很好,翻译的也棒!感谢译者,感谢感谢! iOS-Core-Animation-Advanced-Techni...
    钱嘘嘘阅读 6,897评论 0 6
  • 经历过恶意才会懂得收敛 今天有个姑娘问我 你有放不下的人吗 我说我没有 可是等我独自翻了翻那些好久之前难熬的夜晚 ...
    晴朗的人阅读 1,544评论 0 2
  • 【敬畏】-【体验】-【持续】-【交给】-【显现】 1、缺啥补啥,怕啥练啥 2、一切为我所用,所用为团队 3、我要变...
    魏晋凯阅读 1,373评论 0 0

友情链接更多精彩内容