【问题标题】:Scrolling text like iTunes in Cocoa在 Cocoa 中像 iTunes 一样滚动文本
【发布时间】:2013-11-04 04:20:00
【问题描述】:

我已阅读答案:iTunes Song Title Scrolling in Cocoa

这是我写的代码:

// 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

然后我将一个 NSView 拖到 Interface Builder 的主窗口中,并将其类更改为“ScrollingTextView”。 在我做的控制器中:

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

但是当我运行它时什么也没发生,你能帮我一把吗?谢谢!

【问题讨论】:

    标签: objective-c cocoa nsview


    【解决方案1】:
    ScrollingTextView *test = [[ScrollingTextView alloc] init];
    

    这样做意味着您正在创建一个新的 ScrollingTextView,而不是引用在 .xib 中创建和初始化的一个 Interface Builder

    在您的 viewcontroller 中将 ScrollingTextView 设置为 IBOutlet

    (IBOutlet)ScrollingTextView *test;
    

    从界面构建器中分配您拖入其中的 CustomView,作为视图控制器的引用出口。这就是您初始化 ScrollingTextView 类的方式。

    现在调用你的 setTest: 和 setSpeed: 应该可以正常工作。

    【讨论】:

    • 所以你的意思是我应该这样做ScrollingTextView *test;?如果我不在我的控制器中初始化它,ScrollingTextView 似乎无法接收我设置的值。 ;-)
    • 是的,这行得通!但是CPU使用率似乎很高,您可以留下您的电子邮件或给我发一封我想与您沟通的邮件吗?谢谢
    • @elilien 可能是因为 '[test setSpeed:0.01];'尝试更高的值。
    • 谢谢,这个我知道,但我想可能是算法问题造成的
    猜你喜欢
    • 2011-03-15
    • 2010-11-20
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    相关资源
    最近更新 更多