I'm trying to make an app that simply draws a line on a window that reflects the position of two touches on a trackpad. Here's the code:
Oh, quick note, startPoint and endPoint are both CGPoint iVars. This is more just me experimenting with NSTouches and Core Graphics, so I'm not too concerned with following proper practices and not having iVars.
If the user moves their fingers quickly enough, the line will fall behind. If the user moves their fingers quickly enough for long enough, they can actually lift their fingers up and watch the line to continue to move for several seconds after the user has stopped.
Suggestions?
Edit: I'm running this in the debugger in Xcode on an Original MacBook Air (1.6 GHz). Even so, it shouldn't matter... I'd rather if it aborted drawing anything it missed in-between and skipped to what it should look like right now.
Oh, quick note, startPoint and endPoint are both CGPoint iVars. This is more just me experimenting with NSTouches and Core Graphics, so I'm not too concerned with following proper practices and not having iVars.
Code:
- (void)drawRect:(NSRect)rect
{
CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetLineCap(myContext, kCGLineCapSquare);
CGContextSetRGBStrokeColor(myContext, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(myContext, 1.0);
CGContextMoveToPoint(myContext, startPoint.x + 0.5, startPoint.y + 0.5);
CGContextAddLineToPoint(myContext, endPoint.x + 0.5, endPoint.y + 0.5);
CGContextStrokePath(myContext);
}
- (void)touchesMovedWithEvent:(NSEvent *)event
{
NSLog(@"Touches moved.");
NSMutableSet *touches = [[event touchesMatchingPhase:NSTouchPhaseAny inView:self] mutableCopy];
NSTouch *firstTouch = (NSTouch *)[touches anyObject];
[touches removeObject:firstTouch];
NSTouch *secondTouch = (NSTouch *)[touches anyObject];
if (secondTouch)
{
float startX = (firstTouch.normalizedPosition.x)*(self.frame.size.width);
float startY = (firstTouch.normalizedPosition.y)*(self.frame.size.height);
startPoint = CGPointMake(startX, startY);
float endX = (secondTouch.normalizedPosition.x)*(self.frame.size.width);
float endY = (secondTouch.normalizedPosition.y)*(self.frame.size.height);
endPoint = CGPointMake(endX, endY);
[self setNeedsDisplay:YES];
}
}
If the user moves their fingers quickly enough, the line will fall behind. If the user moves their fingers quickly enough for long enough, they can actually lift their fingers up and watch the line to continue to move for several seconds after the user has stopped.
Suggestions?
Edit: I'm running this in the debugger in Xcode on an Original MacBook Air (1.6 GHz). Even so, it shouldn't matter... I'd rather if it aborted drawing anything it missed in-between and skipped to what it should look like right now.
Last edited: