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;
}
本文介绍了一个简单的iOS应用中自定义触控视图(MyTouchView)的实现方式,该视图可以响应触摸事件,并根据触摸起始和结束位置动态调整一个子视图(imageV)的大小以实现简易的截图功能。

被折叠的 条评论
为什么被折叠?



