만약 당신이 존재하는 컨트롤에 기능을 구형 화하려고한다면 이것이 어떻게 어려울 수 있는지 알 수 있습니다. 그러나 일반 NSView로 시작한다면 그리 나쁘지 않습니다. 나는
//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
그냥 인터페이스 빌더에서 윈도우에 NSView의를 끌어와 그 클래스에 "ScrollingTextView"로 변경 ... 10 분에이를 채찍질. 이것은 분명히 매우 초보입니다
[myScrollingTextView setText:@"This is the text I want to scroll"];
[myScrollingTextView setSpeed:0.01]; //redraws every 1/100th of a second
,하지만 당신이 찾고있는 물건 주위에 랩을 수행하고 시작하기에 괜찮은 장소 : 그런 (코드), 당신이 할.
챔피언처럼 작동합니다. 나는 인터넷에서 비슷한 예를 찾을 수 없다는 것을 믿을 수 없다. 어쩌면 당신의 해결책을 보는 것이 너무 단순 할 수도 있습니다. –
그 종류의 UI 안티 패턴 때문일 것입니다. 몇 가지 이유로 사용자는 특정 시점에 사용자가보고있는 경우 원하는 정보를 보여줄 것입니까? 아마도 그렇지 않습니다. – EricLeaf