0

This is my first time really using cocoa to make an application for mac. The only problem is that drawRect isn't being called at all. The real kicker is that it used to, but now it doesn't, and I have no idea what I did wrong. :/ I am setting 'setNeedsDisplay' but it refuses to work. As you can see, I have two debug prints in there, but only "DEBUG 1" is being called. Any ideas?

//ScrollingTextView.h:
//Adapted from http://stackoverflow.com/a/3233802/3438793

#import <Cocoa/Cocoa.h>

@interface ScrollingTextView : NSView {
    NSTimer *scroller;
    NSPoint point;
    NSString *text;
    NSString *tempText;
    NSString *nextText;
    CGFloat stringWidth;
    NSInteger delay;
    BOOL draw;
    NSDictionary *fontDict;
    NSFont *font;
    BOOL isBigger;
}

@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *tempText;
@property (nonatomic, copy) NSString *nextText;
@property (nonatomic) NSInteger delay;
@property (nonatomic) BOOL draw;
@property (nonatomic) NSDictionary *fontDict;
@property (nonatomic) NSFont *font;
@property (nonatomic) BOOL isBigger;

@end


//ScrollingTextView.m
//Adapted from http://stackoverflow.com/a/3233802/3438793

#import "ScrollingTextView.h"

@implementation ScrollingTextView

@synthesize text;
@synthesize tempText;
@synthesize nextText;
@synthesize delay;
@synthesize draw;
@synthesize fontDict;
@synthesize font;
@synthesize isBigger;

- (void) setNextText:(NSString *)newText {
    font = [NSFont fontWithName:@"Lucida Grande" size:15.0];
    fontDict = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];

    if (text == nil) {
        text = [newText copy];
    } else {
        nextText = [newText copy];
    }

    point = NSZeroPoint;

    stringWidth = [newText sizeWithAttributes:fontDict].width;

    if (stringWidth <= 163) {
        NSString *size = [@"{" stringByAppendingString:[[NSString stringWithFormat:@"%f", stringWidth] stringByAppendingString:@", 22}"]];
        [self setFrameSize:NSSizeFromString(size)];
        isBigger = false;
    } else {
        isBigger = true;
    }

    if (text != nil) {
        scroller = nil;
        scroller = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(moveText:) userInfo:nil repeats:YES];
    }
    draw = false;
    delay = 100;
}

- (void) moveText:(NSTimer *)timer {
    point.x = point.x - 1.0f;
    [self setNeedsDisplay:YES];
    NSLog(@"DEBUG 1");
}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.

    NSLog(@"DEBUG 2");
    //NSLog([@"Next: " stringByAppendingString:nextText]);
    //NSLog([@"Temp: " stringByAppendingString:tempText]);
    //NSLog([@"Main: " stringByAppendingString:text]);

    if (isBigger) {
        if (draw) {
            CGFloat pointX = dirtyRect.size.width + (-1*(dirtyRect.size.width - stringWidth)) + 30;

            if (point.x < -1*pointX) {
                point.x += pointX;
                draw = false;
                delay = 100;
                text = tempText;
                tempText = nil;
            }

            [text drawAtPoint:point withAttributes:fontDict];

            if (point.x < 0) {
                NSPoint otherPoint = point;
                otherPoint.x += pointX;
                if (tempText != nil) {
                    [tempText drawAtPoint:otherPoint withAttributes:fontDict];
                } else {
                    [text drawAtPoint:otherPoint withAttributes:fontDict];
                }
            }
        } else {
            if (nextText != nil) {
                tempText = nextText;
                nextText = nil;
            }

            point.x = 0;
            [text drawAtPoint:point withAttributes:fontDict];

            if (delay <= 0) {
                draw = true;
            } else {
                delay -= 1;
            }
        }
    } else {
        dirtyRect.size.width = stringWidth;
        point.x = 0;

        text = nextText;
        [text drawAtPoint:point withAttributes:fontDict];
    }
}

@end

EDIT: I added a line to "moveText" to see what "needsDisplay" was, and it turns out that "[self setNeedsDisplay:YES];" is doing nothing. Here is the line I added:

- (void) moveText:(NSTimer *)timer {
    point.x = point.x - 1.0f;
    [self setNeedsDisplay:YES];
    NSLog([NSString stringWithFormat:@"%hhd", [self needsDisplay]]); //This one
}

It just prints 0

thislooksfun
  • 1,049
  • 10
  • 23

0 Answers0