- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"恭喜您中奖了";
[self.view addSubview:label];
self.imageView = [[UIImageView alloc] init];
self.imageView.image = [self imageWithColor:[UIColor grayColor]];
self.imageView.frame = CGRectMake(100, 100, 200, 50);
[self.view addSubview:self.imageView];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self.imageView];
CGRect rect = CGRectMake( point.x - 10,point.y - 10, 20, 20);
UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);
CGContextRef ref = UIGraphicsGetCurrentContext();
[self.imageView.layer renderInContext:ref];
CGContextClearRect(ref, rect);
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView.image = image;
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}