Posso vedere come questo sarebbe difficile se si sta tentando di calzare la funzionalità in un controllo esistente. Tuttavia, se inizi con un semplice NSView, non è poi così male. Ho sbattuto questo in su in circa 10 minuti ...
//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;
- (void) dealloc {
[text release];
[scroller invalidate];
[super dealloc];
}
- (void) setText:(NSString *)newText {
[text release];
text = [newText copy];
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;
[scroller invalidate];
scroller == nil;
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.
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
È sufficiente trascinare un NSView sulla vostra finestra in Interface Builder e cambiare categoria per "ScrollingTextView". Poi (in codice), si fa:
[myScrollingTextView setText:@"This is the text I want to scroll"];
[myScrollingTextView setSpeed:0.01]; //redraws every 1/100th of a second
Questo è ovviamente piuttosto rudimentale, ma lo fa avvolgere intorno roba che state cercando ed è un posto decente per iniziare.
fonte
2010-07-13 02:34:33
Funziona come un campione. Non posso credere di non aver trovato esempi simili su internet. Forse è semplicemente troppo semplice ora che vedo la tua soluzione. –
Questo è probabilmente il suo tipo di anti-pattern UI. Per alcuni motivi il capo è che in qualsiasi momento se l'utente lo guarda, mostrerà le informazioni che desidera? Probabilmente no. – EricLeaf