NSView drawRect未被调用

时间:2014-07-16 19:55:44

标签: objective-c cocoa

这是我第一次真正使用cocoa来制作mac的应用程序。唯一的问题是drawRect根本就没有被调用。它曾经是真正的踢球者,但现在它并没有,我不知道我做错了什么。 :/ 我正在设置' setNeedsDisplay'但它拒绝工作。正如您所看到的,我在那里有两个调试打印,但只有#34; DEBUG 1"被称为。有任何想法吗?

//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
编辑:我在&#34; moveText&#34;中添加了一行代码。看看&#34; needsDisplay&#34;是的,事实证明&#34; [self setNeedsDisplay:YES];&#34;什么也没做。这是我添加的行:

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

它只打印0

0 个答案:

没有答案