刮一刮擦除效果

1.前言

之前需求有個(gè)刮一刮出答案的功能,做個(gè)記錄,希望對(duì)需要的同學(xué)有幫助。

實(shí)現(xiàn)原理

給展示的view添加CAShapeLayer的mask,當(dāng)我們滑動(dòng)的時(shí)候,把軌跡添加到path上,添加的地方就可以顯示出來(lái)。
不太懂mask的同學(xué)可以看ios CALayer之mask使用。

2.實(shí)現(xiàn)方法

實(shí)現(xiàn)方法如下

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移動(dòng)添加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 ;//超過(guò)0.75則不用滑動(dòng)
    [self showContentView];
    self.userInteractionEnabled = NO;
}

2.3計(jì)算透明占比

截圖然后計(jì)算圖片透明像素占比

- (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.補(bǔ)充

這里也有一種比較生硬的方式

-(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);
}

這種也可以實(shí)現(xiàn),但是流暢度比較差,另外計(jì)算像素透明度會(huì)有很大的偏差。

4.demo

demo鏈接

參考資料

ios CALayer之mask使用
一個(gè)萬(wàn)能的刮刮樂(lè)控件

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒(méi)有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,621評(píng)論 1 32
  • 參考文檔:https://pan.baidu.com/s/1HaQRu8c8bNfKTSbxF5__Sw相同內(nèi)容網(wǎng)...
    liboxiang閱讀 521評(píng)論 0 1
  • 書(shū)寫(xiě)的很好,翻譯的也棒!感謝譯者,感謝感謝! iOS-Core-Animation-Advanced-Techni...
    錢(qián)噓噓閱讀 2,428評(píng)論 0 6
  • 經(jīng)歷過(guò)惡意才會(huì)懂得收斂 今天有個(gè)姑娘問(wèn)我 你有放不下的人嗎 我說(shuō)我沒(méi)有 可是等我獨(dú)自翻了翻那些好久之前難熬的夜晚 ...
    晴朗的人閱讀 224評(píng)論 0 2
  • 【敬畏】-【體驗(yàn)】-【持續(xù)】-【交給】-【顯現(xiàn)】 1、缺啥補(bǔ)啥,怕啥練啥 2、一切為我所用,所用為團(tuán)隊(duì) 3、我要變...
    魏晉凱閱讀 189評(píng)論 0 0

友情鏈接更多精彩內(nèi)容