4. 图片的擦除效果

2023-03-09  本文已影响0人  写代码的向日葵

- (instancetype)initWithFrame:(CGRect)frame{
    self.userInteractionEnabled = YES;
    if(self = [super initWithFrame:frame]){
        UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(gestureRecognizer:)];
        [self addGestureRecognizer:gestureRecognizer];
    }
    return self;
}


-(void)gestureRecognizer:(UIPanGestureRecognizer*)pan{
    CGFloat w = 30;
    CGPoint point = [pan locationInView:self];
    CGFloat x = point.x - w * 0.5;
    CGFloat y = point.y - w * 0.5;
    CGRect rect = CGRectMake(x, y, w, w);
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    CGContextClearRect(ctx, rect);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.image = newImage;
}
上一篇 下一篇

猜你喜欢

热点阅读