Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-20-2011, 08:44 PM   #1 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default drawRect isn't getting called

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.
__________________
If I have helped you, please consider donating. I use PayPal. It would mean a lot to me!


For more iOS Development, check out my blog—Cups of Cocoa. All visitors welcome, but especially beginners!

Hope I have helped!

Please check out IceFall, a new action game available in the App Store!
musicwind95 is offline   Reply With Quote
Old 05-21-2011, 08:02 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: UK, North East
Posts: 227
zardon is on a distinguished road
Default

You have:

Code:
// View Controller Code
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// ...
        [(TrackerView *)self.view draw];
}
But your draw method only has:

Code:
- (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];
}
The draw method never runs [self drawRect:...]

Unless its elsewhere?

Put breakpoints in inside the draw method (not drawRect and use the step-by-step tool to see what/where it calls next (if anything).
zardon is offline   Reply With Quote
Old 05-21-2011, 09:31 PM   #3 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

I'm calling setNeedsDisplay in the setCurrentPoint code. I've tried breakpoints, but they haven't told me anything I didn't already know.
__________________
If I have helped you, please consider donating. I use PayPal. It would mean a lot to me!


For more iOS Development, check out my blog—Cups of Cocoa. All visitors welcome, but especially beginners!

Hope I have helped!

Please check out IceFall, a new action game available in the App Store!
musicwind95 is offline   Reply With Quote
Reply

Bookmarks

Tags
acceleration, draw image in cg, point, quartz

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 360
7 members and 353 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, PlutoPrime, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0