图片擦除(支付宝擦除效果)

2016-12-01  本文已影响14人  遇见灬最美的你

原理就是俩张图片叠在一起,去除上面图片的效果。

000.gif

代码如下

//
//  ViewController.m
//  05-图片擦除(了解)
//
//  Created by 李亮 on 2016/12/1.
//  Copyright © 2016年 www.thelast.com. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imgView.userInteractionEnabled = YES;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    
    [self.imgView addGestureRecognizer:pan];
}

- (void)pan:(UIPanGestureRecognizer *)pan{
    
    CGPoint p = [pan locationInView:self.imgView];
    
    CGFloat wh = 20;
    CGFloat x = p.x - wh * 0.5;
    CGFloat y = p.y - wh * 0.5;

    CGRect rect = CGRectMake(x, y, wh, wh);
    
    //获取图片的上下文
    UIGraphicsBeginImageContextWithOptions(self.imgView.bounds.size, NO, 0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.imgView.layer renderInContext:ctx];
    CGContextClearRect(ctx, rect);
    
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.imgView.image = newImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

代码链接

下载地址 demo

上一篇 下一篇

猜你喜欢

热点阅读