Quartz2D _ 刮刮乐效果

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

推荐阅读更多精彩内容