Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// LHDFingerpaintView.m
// Drawing
//
// Created by Steven Masuch on 2014-07-31.
// Copyright (c) 2014 Lighthouse Labs. All rights reserved.
//
#import "LHDFingerpaintView.h"
@interface LHDTouchPair : NSObject
@property (nonatomic) CGPoint touch1Point;
@property (nonatomic) CGPoint touch2Point;
@end
@implementation LHDTouchPair
@end
@interface LHDFingerpaintView ()
@property (nonatomic) NSMutableArray * touchPairs;
@end
@implementation LHDFingerpaintView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_touchPairs = [NSMutableArray array];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// We don't need to do anything here
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Here's the meat of this
for (UITouch *touch in touches) {
LHDTouchPair *touchPair = [[LHDTouchPair alloc] init];
touchPair.touch1Point = [touch locationInView:self];
touchPair.touch2Point = [touch previousLocationInView:self];
[self.touchPairs addObject:touchPair];
}
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 5.0);
for (LHDTouchPair *pair in self.touchPairs) {
CGContextBeginPath(context);
CGContextMoveToPoint(context, pair.touch1Point.x, pair.touch1Point.y);
CGContextAddLineToPoint(context, pair.touch2Point.x, pair.touch2Point.y);
CGContextStrokePath(context);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment