刮开涂层

类似于刮奖的小功能,废话不多说直接上代码

-(UILabel*)textLbl{

if (_textLbl==nil ) {

_textLbl=[[UILabel alloc]init];

_textLbl.text=@"谢谢惠顾!";

_textLbl.frame=CGRectMake(100, 200, 200, 100);

_textLbl.textAlignment=NSTextAlignmentCenter;

}

return _textLbl;

}

-(UIImageView *)coatingImg{

if (_coatingImg==nil) {

_coatingImg=[[UIImageView alloc]init];

_coatingImg.image=[UIImage imageNamed:@"photo.jpg"];

_coatingImg.frame=self.textLbl.frame;

}

return _coatingImg;

}

- (void)touchesMoved:(NSSet*)touches withEvent:(nullable UIEvent *)event{

// 触摸任意位置

UITouch *touch = touches.anyObject;

// 触摸位置在图片上的坐标

CGPoint cententPoint = [touch locationInView:self.coatingImg];

// 设置清除点的大小

CGRect  rect = CGRectMake(cententPoint.x, cententPoint.y, 20, 20);

// 默认是去创建一个透明的视图

UIGraphicsBeginImageContextWithOptions(self.coatingImg.bounds.size, NO, 0);

// 获取上下文(画板)

CGContextRef ref = UIGraphicsGetCurrentContext();

// 把imageView的layer映射到上下文中

[self.coatingImg.layer renderInContext:ref];

// 清除划过的区域

CGContextClearRect(ref, rect);

// 获取图片

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

// 结束图片的画板, (意味着图片在上下文中消失)

UIGraphicsEndImageContext();

self.coatingImg.image = image;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 蛾儿雪柳黄金缕,笑语盈盈暗香去!<波克比> 总结一个小的Demo,先看效果图. 刮开涂层效果.gif 设置刮开后,...
    云之君兮鹏阅读 7,585评论 31 162
  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 808评论 0 3
  • Quartz2D 简介 Quartz2D是二维(平面)的绘图引擎(经包装的函数库,方便开发者使用。也就是说苹果帮我...
    iOS_Cqlee阅读 640评论 0 2
  • 就是创建一个UILabel,再创建一个UIImageView盖住,然后用touchedMoved方法对imagev...
    和珏猫阅读 834评论 0 2
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 838评论 0 0