iOS 仿支付宝刮刮乐效果
生活随笔
收集整理的这篇文章主要介绍了
iOS 仿支付宝刮刮乐效果
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
支付宝里有个刮刮乐中奖, 和大街小巷里的类似彩票刮刮乐的效果一样。
实现思路, 其实很简单的三步:
虽然思路简单,但是还需要注意:
下面就直接上代码!
- (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.设置遮挡在外面的Image(被刮的图片)UIImageView *scratchedImg = [[UIImageView alloc] initWithFrame:showLabel.frame];scratchedImg.image = [UIImage imageNamed:@"scratched"];[self.view addSubview:scratchedImg];self.scratchedImg = scratchedImg; } 复制代码// 3.在touchesMoved方法里面实现操作 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {// 触摸任意位置UITouch *touch = touches.anyObject;// 触摸位置在图片上的坐标CGPoint cententPoint = [touch locationInView:self.scratchedImg];// 设置清除点的大小CGRect rect = CGRectMake(cententPoint.x, cententPoint.y, 20, 20);// 默认是去创建一个透明的视图UIGraphicsBeginImageContextWithOptions(self.scratchedImg.bounds.size, NO, 0);// 获取上下文(画板)CGContextRef ref = UIGraphicsGetCurrentContext();// 把imageView的layer映射到上下文中[self.scratchedImg.layer renderInContext:ref];// 清除划过的区域CGContextClearRect(ref, rect);// 获取图片UIImage *image = UIGraphicsGetImageFromCurrentImageContext();// 结束图片的画板, (意味着图片在上下文中消失)UIGraphicsEndImageContext();self.scratchedImg.image = image; } 复制代码贴上 Demo , 如果需要直接这里下载就可以喽~
总结
以上是生活随笔为你收集整理的iOS 仿支付宝刮刮乐效果的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: css案例学习之按钮超链接
- 下一篇: 显示日期及星期