ViewController.m
@interface ViewController (){
MyTouchView *touchView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
touchView = [[MyTouchView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
touchView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"liushishi"]];
touchView.userInteractionEnabled = YES;
touchView.multipleTouchEnabled = YES;
[self.view addSubview:touchView];
}
myTouchView.m
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
imageV = [[UIView alloc] init];
imageV.backgroundColor = [UIColor blackColor];
imageV.alpha = 0.5;
[self addSubview:imageV];
}
return self;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_startPoint = [touch locationInView:self];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
_endPoint = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGFloat width = _endPoint.x - _startPoint.x;
CGFloat height = _endPoint.y - _startPoint.y;
CGRect area = CGRectMake(_startPoint.x, _startPoint.y, width, height);
imageV.frame = area;
}