iOS基礎(chǔ)-小Demo--刮開涂層(刮刮樂效果)

蛾兒雪柳黃金縷,笑語盈盈暗香去!<波克比>


總結(jié)一個(gè)小的Demo,先看效果圖.

刮開涂層效果.gif

  1. 設(shè)置刮開后,顯示的文字Label

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(7, 50, 400, 400)];
label.text = @"離思五首\n元稹\n曾經(jīng)滄海難為水,\n除卻巫山不是云!\n取次花叢懶回顧,\n半緣修道半緣君!\n";
label.numberOfLines = 0;
label.backgroundColor = [UIColor colorWithRed:(arc4random()%173)/346.0 + 0.5 green:(arc4random()%173)/346.0 + 0.5 blue:(arc4random()%173)/346.0 + 0.5 alpha: 1];
label.font = [UIFont systemFontOfSize:30];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];

2. 設(shè)置遮擋在外面的Image
>```obj
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(7, 50, 400, 400)];
    self.imageView.image = [UIImage imageNamed:@"可達(dá)鴨"];
    [self.view addSubview:self.imageView ];```
3. 在touchesMoved方法里面實(shí)現(xiàn)操作
>```obj
`- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // 觸摸任意位置
    UITouch *touch = touches.anyObject;
    // 觸摸位置在圖片上的坐標(biāo)
    CGPoint cententPoint = [touch locationInView:self.imageView];
    // 設(shè)置清除點(diǎn)的大小
    CGRect  rect = CGRectMake(cententPoint.x, cententPoint.y, 20, 20);
    // 默認(rèn)是去創(chuàng)建一個(gè)透明的視圖
UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);  
    // 獲取上下文(畫板)
    CGContextRef ref = UIGraphicsGetCurrentContext();
       // 把imageView的layer映射到上下文中
    [self.imageView.layer renderInContext:ref];
    // 清除劃過的區(qū)域
    CGContextClearRect(ref, rect);  
    // 獲取圖片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 結(jié)束圖片的畫板, (意味著圖片在上下文中消失)
    UIGraphicsEndImageContext();
    self.imageView.image = image;```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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