iOS開(kāi)發(fā) - 刮刮樂(lè)效果的實(shí)現(xiàn)

引言

我們平時(shí)在使用支付寶的時(shí)候, 會(huì)看到類似彩票刮刮樂(lè)的效果。如何實(shí)現(xiàn)的呢?下面就直接上代碼了...

刮刮樂(lè)效果:

1.創(chuàng)建視圖:

-(void)createSubViews {
    /**
            注意:
        1. 這兩個(gè)控件的位置要相同
        2. 一定要先創(chuàng)建下面的label, 再創(chuàng)建圖片
     */
    
    // 展示刮出來(lái)的效果的view
    UILabel *labelL        = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 200)];
    labelL.text            = @"刮刮樂(lè)效果展示";
    labelL.numberOfLines   = 0;
    labelL.backgroundColor = [UIColor brownColor];
    labelL.font            = [UIFont systemFontOfSize:30];
    labelL.textAlignment   = NSTextAlignmentCenter;
    [self.view addSubview:labelL];
    
    // 被刮的圖片
    self.imageView       = [[UIImageView alloc] initWithFrame:labelL.frame];
    self.imageView.image = [UIImage imageNamed:@"222.jpg"];
    [self.view addSubview:self.imageView];
}

2.實(shí)現(xiàn)刮刮樂(lè)效果:

-(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);
    // 獲取上下文(畫(huà)板)
    CGContextRef ref = UIGraphicsGetCurrentContext();
    // 把imageView的layer映射到上下文中
    [self.imageView.layer renderInContext:ref];
    // 清除劃過(guò)的區(qū)域
    CGContextClearRect(ref, rect);
    // 獲取圖片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 結(jié)束圖片的畫(huà)板, (意味著圖片在上下文中消失)
    UIGraphicsEndImageContext();
    
    self.imageView.image = image;
}

效果圖:

效果圖

GitHub: https://github.com/LiCheng244/LCUtils
個(gè)人博客: http://www.licheng244.com/


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評(píng)論 25 708
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 46,901評(píng)論 22 665
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 32,965評(píng)論 6 472
  • 聽(tīng)說(shuō) 卡爾加里的春姑娘已從冬眠醒來(lái) 你看 嫩芽正從枝梢探出頭 野兔已褪去一身白衫 你聽(tīng) 知更鳥(niǎo)正在枝頭歡唱 貓頭鷹...
    氣和神瑩閱讀 240評(píng)論 5 2
  • 當(dāng)發(fā)生某件事情的時(shí)候, 1、首先,始終懷著相信支持的心態(tài):相信他是一個(gè)負(fù)責(zé)任、開(kāi)放、有擔(dān)當(dāng)……的人(B點(diǎn)),他目前...
    sageness閱讀 203評(píng)論 0 0