2010-07-12 12 views
6

나는 광범위하게 검색했으며 텍스트가 코코아에서 너무 큰 경우 iTunes 노래 제목 스크롤과 비슷한 효과를 얻는 방법에 대한 정보를 찾지 못했습니다. NSTextField에서 한계를 설정하려고 시도했습니다. NSScrollView를 사용하여 다양한 시도뿐만 아니라 NSTextView 사용하여 시도했다. 나는 단순한 무언가를 놓치고 있다고 확신하지만 어떤 도움이라도 대단히 감사 할 것입니다. 가능한 경우 CoreGraphics를 사용할 필요가 없기를 바라고 있습니다.iTunes 노래 제목 코코아에서 스크롤

Example, "Base.FM http://www"을 확인하십시오. 텍스트가 스크롤되었습니다. 더 좋은 예가 필요한 경우 다소 큰 제목이있는 노래로 iTunes를 열고 앞뒤로 스크롤하십시오.

NSTextField와 NSTimer를 사용하여 선택 윤곽 유형 효과를 만들 수있는 간단한 방법이 있습니다.하지만 슬프지만.

답변

18

만약 당신이 존재하는 컨트롤에 기능을 구형 화하려고한다면 이것이 어떻게 어려울 수 있는지 알 수 있습니다. 그러나 일반 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 

,하지만 당신이 찾고있는 물건 주위에 랩을 수행하고 시작하기에 괜찮은 장소 : 그런 (코드), 당신이 할.

+0

챔피언처럼 작동합니다. 나는 인터넷에서 비슷한 예를 찾을 수 없다는 것을 믿을 수 없다. 어쩌면 당신의 해결책을 보는 것이 너무 단순 할 수도 있습니다. –

+0

그 종류의 UI 안티 패턴 때문일 것입니다. 몇 가지 이유로 사용자는 특정 시점에 사용자가보고있는 경우 원하는 정보를 보여줄 것입니까? 아마도 그렇지 않습니다. – EricLeaf

관련 문제