1

I've read the answer at: iTunes Song Title Scrolling in Cocoa

And here is the code I written:

// ScrollingTextView.h
#import <Cocoa/Cocoa.h>

@interface ScrollingTextView : NSView {
    NSTimer *scroller;
    NSPoint point;
    NSString *text;
    NSTimeInterval speed;
    CGFloat stringWidth;
}

@property (nonatomic, copy) NSString *text;
@property (nonatomic) NSTimeInterval speed;

@end

// ScrollingTextView.m
#import "ScrollingTextView.h"

@implementation ScrollingTextView

@synthesize text;
@synthesize speed;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)dealloc {
    [scroller invalidate];
}

- (void)setText:(NSString *)newText {
    text = [newText copy];
    NSLog(@"t: %@", text);
    point = NSZeroPoint;

    stringWidth = [newText sizeWithAttributes:nil].width;

    if (scroller == nil && speed > 0 && text != nil) {
        scroller = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(moveText:) userInfo:nil repeats:YES];
    }
}

- (void)setSpeed:(NSTimeInterval)newSpeed {
    if (newSpeed != speed) {
        speed = newSpeed;
        NSLog(@"s: %f", speed);
        [scroller invalidate];
        if (speed > 0 && text != nil) {
            scroller = [NSTimer scheduledTimerWithTimeInterval:speed target:self selector:@selector(moveText:) userInfo:nil repeats:YES];
        }
    }
}

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

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
    [super drawRect:dirtyRect];
    if (point.x + stringWidth < 0) {
        point.x += dirtyRect.size.width;
    }

    [text drawAtPoint:point withAttributes:nil];

    if (point.x < 0) {
        NSPoint otherPoint = point;
        otherPoint.x += dirtyRect.size.width;
        [text drawAtPoint:otherPoint withAttributes:nil];
    }
}

@end

Then I drag an NSView onto my main window in Interface Builder and change its class to "ScrollingTextView". In the controller I do:

ScrollingTextView *test = [[ScrollingTextView alloc] init];
[test setText:@"Test long text scrolling!"];
[test setSpeed:0.01];

But nothing happened when I run it, can you give me a hand? Thank you!

Community
  • 1
  • 1
elilien
  • 13
  • 2

1 Answers1

0
ScrollingTextView *test = [[ScrollingTextView alloc] init];

Doing this means you are creating a new ScrollingTextView and not referencing the one Interface Builder created and initialized in the .xib

Setup ScrollingTextView as an IBOutlet in your viewcontroller.

(IBOutlet)ScrollingTextView *test;

From the interface builder assign the CustomView you dragged into it, as a referencing outlet to the view controller. This is how you initialize the ScrollingTextView class.

Now call your setTest: and setSpeed: It should work fine.

lead_the_zeppelin
  • 2,017
  • 13
  • 23
  • So you mean I should do this `ScrollingTextView *test;`? And if I don't init it in my controller, the ScrollingTextView seems cannot receive the value I set. ;-) – elilien Nov 04 '13 at 04:57
  • yup, that works! But the CPU usage seems so high, and can you leave up your email or send me a mail that I want to communicate with you about this? Thanks – elilien Nov 04 '13 at 05:21
  • @elilien it might be because of '[test setSpeed:0.01];' Try a higher value. – lead_the_zeppelin Nov 04 '13 at 05:31
  • Thanks, I know this, but I think it maybe caused by some algorithm problem – elilien Nov 04 '13 at 05:34