#import "ViewController.h" @interface ViewController () @property (nonatomic, weak) UIView *cover; @property (nonatomic, assign) CGPoint oriP; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (weak, nonatomic) IBOutlet UIView *view1; @end @implementation ViewController //- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //{ // CGRect frame = _view1.frame; //// frame.size.width = -100; //// _view1.frame = frame; ////} - (UIView *)cover { if (_cover == nil) { UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor blackColor]; view.alpha = 0.5; _cover = view; [self.view addSubview:view]; } return _cover; } - (IBAction)pan:(UIPanGestureRecognizer *)sender { // 获取下当前的触摸 CGPoint curP = [sender locationInView:_imageView]; if (sender.state == UIGestureRecognizerStateBegan) { // 记录下一开始的位置 _oriP = curP; } // 计算下黑色蒙版的frame CGFloat w = curP.x - _oriP.x; CGFloat h = curP.y - _oriP.y; self.cover.frame = CGRectMake(_oriP.x, _oriP.y, w, h); if (sender.state == UIGestureRecognizerStateEnded) { // 手指抬起 // 裁剪图片,生成一张新图片 // 开启位图上下文 UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0); // 设置裁剪区域 UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.cover.frame]; [path addClip]; // 绘制图片 [_imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; // 生成图片 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // 关闭上下文 UIGraphicsEndImageContext(); _imageView.image = image; [self.cover removeFromSuperview]; } } - (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:)]; // // [_imageView addGestureRecognizer:pan]; } //- (void)pan:(UIPanGestureRecognizer *)pan //{ // NSLog(@"%s",__func__); //} @end