如何在我的iPhone应用程序中实现Ticker

时间:2010-07-02 10:08:28

标签: iphone objective-c

我只想在我的应用程序中使用自动收报机, 我不知道要实现自动收报机,请告诉我。

由于

2 个答案:

答案 0 :(得分:5)

您需要做的一切都在SDK中,根本不需要自定义。我没有检查过这个,但您可以尝试以下方法:

#import <UIKit/UIKit.h>


@interface TickerScrollView : UIScrollView {

    UILabel *textLabel;

}

- (void)displayText:(NSString *)string;
- (void)clearTicker;

@property (nonatomic, retain, readwrite) UILabel *textLabel;

@end

////


#import "TickerScrollView.h"

@interface TickerScrollView()

- (void)initialiseTextLabel;
- (void)clearTicker;

- (void)beginAnimation;

@end


@implementation TickerScrollView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {

        // Initialization code
        [self setFrame: frame];

        [self setBounces: NO];
        [self setUserInteractionEnabled:NO];

        [self setShowsVerticalScrollIndicator:NO];
        [self setShowsHorizontalScrollIndicator:NO];

        [self initialiseTextLabel];

    }
    return self;
}

- (void)initialiseTextLabel {

    textLabel = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
    [textLabel setTextAlignment:UITextAlignmentLeft];
    [textLabel setNumberOfLines:1];
    [textLabel sizeToFit];

    [self addSubview:textLabel];
    [self sendSubviewToBack:textLabel];

    [self setScrollEnabled:YES];

}

- (void)displayText:(NSString *)string {

    [self clearTicker];

    [textLabel setText:string];
    [textLabel sizeToFit];

    [self setContentSize:textLabel.frame.size];

    [self beginAnimation];

}

- (void)clearTicker {

    [textLabel setText:@""];
    [textLabel sizeToFit];

    CGPoint origin = CGPointMake(0, 0);
    [self setContentOffset:origin];

}

- (void)beginAnimation {

    CGFloat text_width = textLabel.frame.size.width;
    CGFloat display_width = self.frame.size.width;

    if ( text_width > display_width ) {

        CGPoint origin = CGPointMake(0, 0);
        [self setContentOffset:origin];

        CGPoint terminal_origin = CGPointMake(textLabel.frame.size.width - self.frame.size.width, textLabel.frame.origin.y);
        float duration = (text_width - display_width)/50;

        [UIView beginAnimations:nil context:NULL];

        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDelay:1.0];
        [UIView setAnimationDuration:duration];

        [self setContentOffset:terminal_origin];

        [UIView commitAnimations];

    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {

    [textLabel release];
    [super dealloc];

}

@synthesize textLabel;

@end

答案 1 :(得分:1)

假设“Ticker”是指水平滚动的文字:

自动收报机基本上只是一个文本字符串,它的x坐标连续变化而移动。查看这个简单的教程,了解如何显示标签:

http://knol.google.com/k/iphone-sdk-helloworld

之后您可以使用NSTimer调用一个方法来连续更新标签x坐标。