Ale*_*ton 7 cocoa itunes objective-c nstextfield nsscrollview
我已经进行了广泛的搜索,并且在我的生活中找不到任何关于如果在Cocoa中文本太大而如何实现与iTunes歌曲标题滚动类似效果的信息.我试过在NSTextField上设置界限无济于事.我尝试过使用NSTextView以及使用NSScrollView的各种尝试.我相信我错过了一些简单的东西,但任何帮助都会非常感激.我也希望如果可能的话不必使用CoreGraphics.
例如,请注意"Base.FM http:// www." 文本已滚动.如果您需要一个更好的示例,请打开带有相当大标题的歌曲的iTunes,并观看它来回滚动.
我想肯定有一个简单的办法只有创建具有的NSTextField和一个NSTimer,但可惜选取框型的效果.
Dav*_*ong 19
如果你试图将功能强加到一个存在的控件中,我可以看到这将是多么困难.但是,如果你只是从一个简单的NSView开始,它就不那么糟糕了.我在大约10分钟内掀起了这个......
//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
Run Code Online (Sandbox Code Playgroud)
只需在Interface Builder中将NSView拖到窗口上,然后将其类更改为"ScrollingTextView".然后(在代码中),你做:
[myScrollingTextView setText:@"This is the text I want to scroll"];
[myScrollingTextView setSpeed:0.01]; //redraws every 1/100th of a second
Run Code Online (Sandbox Code Playgroud)
这显然是非常简陋的,但它确实包含了你正在寻找的东西,并且是一个体面的起点.
归档时间: |
|
查看次数: |
2937 次 |
最近记录: |