-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
start = point;
end = point;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
end = point;
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
end = point;
[self setNeedsDisplay];
}
#import "myView.h"
@interface myView ()
{
CGPoint start;
CGPoint end;
}
@end
@implementation myView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
-(void)drawRect:(CGRect)rect{
if (start.x != end.x && start.y != end.y) {
CGFloat w = fabs(end.x - start.x);
CGFloat h = fabs(end.y - start.y);
CGFloat x = end.x < start.x ? end.x : start.x;
CGFloat y = end.y < start.y ? end.y : start.y;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
CGRectMake(x, y, w, h)];
[[UIColor redColor] setStroke];
[path stroke];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
start = point;
end = point;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
end = point;
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
end = point;
[self setNeedsDisplay];
}
@end