- 業(yè)務邏輯:上下兩張圖片围辙,上方的圖片遮蓋下方的圖片,為上方圖片添加手勢放案,根據(jù)手勢的位置創(chuàng)建一個擦除的小塊姚建,然后開啟位圖,渲染被擦除的圖層卿叽,清除掉pan手勢劃過的區(qū)域桥胞,得到上下文的位圖,最后關閉上下文考婴,將新的圖片賦值給被擦除的圖片贩虾。
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.view addGestureRecognizer:pan];
}
#define clipWH 30
- (void)pan:(UIPanGestureRecognizer *)pan
{
// 獲取當前觸摸點
CGPoint curP = [pan locationInView:self.view];
// 獲取擦除的矩形范圍
CGFloat wh = clipWH;
CGFloat x = curP.x - wh * 0.5;
CGFloat y = curP.y - wh * 0.5;
CGRect rect = CGRectMake(x, y, wh, wh);
// 開啟位圖上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
// 獲取當前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext(
// 渲染控件
[self.imageView.layer renderInContext:ctx];
// 擦除上下文的某一部分
CGContextClearRect(ctx, rect);
// 生成一張圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
self.imageView.image = image;
// 關閉上下文
UIGraphicsEndImageContext();
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者