I'm creating a basic g-meter app (bubble level?) using Quartz. I'm passing in UIAcceleration values to my custom view, but drawRect: never seems to get called, based on my NSLogs. I've even tried calling drawRect: manually (yes, I know that's a bad idea), but even then, when it worked nothing was changing. The code is here:
Code:
// View Controller Code
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// This check works fine
if (![self oldAccelerationIsEqualToNewAcceleration:acceleration]) {
NSLog(@"Set New Acceleration");
[(TrackerView *)self.view setAcceleration:acceleration];
self.previousAcceleration = acceleration;
[(TrackerView *)self.view draw];
}
}
Code:
// Custom View Code
- (void)drawRect:(CGRect)rect
{
// Drawing code
NSLog(@"DrawRect");
[[UIColor blackColor] setFill];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, CGRectMake(currentPoint.x - 10.0, currentPoint.y - 10.0, 20.0f, 20.0f));
CGContextFillPath(context);
}
- (void)setCurrentPoint:(CGPoint)newPoint {
previousPoint = currentPoint;
currentPoint = newPoint;
if (currentPoint.x > self.bounds.size.width) {
currentPoint.x = self.bounds.size.width;
xVelocity = 0;
}
if (currentPoint.y > self.bounds.size.height) {
currentPoint.y = self.bounds.size.height;
yVelocity = 0;
}
CGRect currentImageRect = CGRectMake(currentPoint.x, currentPoint.y,
currentPoint.x,
currentPoint.y);
CGRect previousImageRect = CGRectMake(previousPoint.x, previousPoint.y,
previousPoint.x,
currentPoint.y);
NSLog(@"setCurrentPoint");
NSLog(@"self.currentPoint.x: %g, .y: %g", self.currentPoint.x, self.currentPoint.y);
[self setNeedsDisplayInRect:CGRectUnion(currentImageRect, previousImageRect)];
// [self setNeedsDisplay];
}
- (CGPoint)pointFromCenterWithXOffset:(CGFloat)xOffset yOffset:(CGFloat)yOffset {
return CGPointMake(self.bounds.size.width / 2 + xOffset, self.bounds.size.height / 2 + yOffset);
}
- (void)draw {
NSLog(@"Draw");
static NSDate *lastDrawTime;
if (lastDrawTime != nil)
self.currentPoint = [self pointFromCenterWithXOffset:(acceleration.x * kAccelerationMultiplier) yOffset:(acceleration.y * kAccelerationMultiplier)];
// Update last time with current time
[lastDrawTime release];
lastDrawTime = [[NSDate alloc] init];
}
I'm quite stumped as to why drawRect never seems to get called. The log messages are not showing anything unusual; currentPoint is getting updated with notably different values each time.