刮一刮擦除效果

1.前言

之前需求有個刮一刮出答案的功能,做個記錄,希望對需要的同學有幫助。

實現(xiàn)原理

給展示的view添加CAShapeLayer的mask,當我們滑動的時候,把軌跡添加到path上,添加的地方就可以顯示出來。
不太懂mask的同學可以看ios CALayer之mask使用。

2.實現(xiàn)方法

實現(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移動添加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);
}

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

4.demo

demo鏈接

參考資料

ios CALayer之mask使用
一個萬能的刮刮樂控件

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

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

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

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