#import "ViewController.h"
@interface ViewController ()
@property(strong, nonatomic) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 200, 40)];
label.text = @"刮刮乐, 挂出个帅哥";
label.backgroundColor = [UIColor orangeColor];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 200, 200, 40)];
self.imageView.image = [UIImage imageNamed:@"1"];
[self.view addSubview:self.imageView];
}
//移动手势
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1. 取得一个触摸对象
UITouch *touch = touches.anyObject;
// 取得触摸对象, 在self.imageView视图上的位置
CGPoint point = [touch locationInView:self.imageView];
//2. 设置橡皮擦大小
CGRect rect = CGRectMake(point.x, point.y, 20, 20);
//3. 开启图片上下文
//参数二: 是否不透明
UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);
//4. 获取上下文
CGContextRef cxt = UIGraphicsGetCurrentContext();
//5. 映射, 将imageView的layer层映射到上下文中
[self.imageView.layer renderInContext:cxt];
//6. 清除划过的区域
CGContextClearRect(cxt, rect);
//7. 获取图片的上下文
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//8. 结束图片的展示(图片在上下文中消失)
UIGraphicsEndImageContext();
self.imageView.image = image;
}
@end```
Quartz2D _ 刮刮乐效果
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 蛾儿雪柳黄金缕,笑语盈盈暗香去!<波克比> 总结一个小的Demo,先看效果图. 刮开涂层效果.gif 设置刮开后,...
- 参考Demo:HYScratchCardViewExample GitHub - HoneyLuka/HYScra...