iOS 仿支付宝刮刮乐效果
生活随笔
收集整理的這篇文章主要介紹了
iOS 仿支付宝刮刮乐效果
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
支付寶里有個刮刮樂中獎, 和大街小巷里的類似彩票刮刮樂的效果一樣。
實(shí)現(xiàn)思路, 其實(shí)很簡單的三步:
雖然思路簡單,但是還需要注意:
下面就直接上代碼!
- (void)setupSubViews {// 1.展示刮開出來的效果:顯示的文字 labelUILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 274, 145)];showLabel.center = self.view.center;showLabel.backgroundColor = [UIColor redColor];showLabel.textColor = [UIColor yellowColor];showLabel.text = @"恭喜你中獎了";showLabel.font = [UIFont systemFontOfSize:30];showLabel.textAlignment = NSTextAlignmentCenter;[self.view addSubview:showLabel];// 2.設(shè)置遮擋在外面的Image(被刮的圖片)UIImageView *scratchedImg = [[UIImageView alloc] initWithFrame:showLabel.frame];scratchedImg.image = [UIImage imageNamed:@"scratched"];[self.view addSubview:scratchedImg];self.scratchedImg = scratchedImg; } 復(fù)制代碼// 3.在touchesMoved方法里面實(shí)現(xiàn)操作 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {// 觸摸任意位置UITouch *touch = touches.anyObject;// 觸摸位置在圖片上的坐標(biāo)CGPoint cententPoint = [touch locationInView:self.scratchedImg];// 設(shè)置清除點(diǎn)的大小CGRect rect = CGRectMake(cententPoint.x, cententPoint.y, 20, 20);// 默認(rèn)是去創(chuàng)建一個透明的視圖UIGraphicsBeginImageContextWithOptions(self.scratchedImg.bounds.size, NO, 0);// 獲取上下文(畫板)CGContextRef ref = UIGraphicsGetCurrentContext();// 把imageView的layer映射到上下文中[self.scratchedImg.layer renderInContext:ref];// 清除劃過的區(qū)域CGContextClearRect(ref, rect);// 獲取圖片UIImage *image = UIGraphicsGetImageFromCurrentImageContext();// 結(jié)束圖片的畫板, (意味著圖片在上下文中消失)UIGraphicsEndImageContext();self.scratchedImg.image = image; } 復(fù)制代碼貼上 Demo , 如果需要直接這里下載就可以嘍~
總結(jié)
以上是生活随笔為你收集整理的iOS 仿支付宝刮刮乐效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css案例学习之按钮超链接
- 下一篇: 显示日期及星期