简体   繁体   中英

How to implement Ticker in my application in iphone

I just want in my application a ticker, i have no idea to implement ticker please tell me.

Thanks

Everything you need to do this is in the SDK, no need to customize at all. I haven't checked this, but you can try the following:

#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

Assuming by "Ticker" you mean a horizontally scrolling text:

A ticker is basically just a text string that is moving by having its x coordinate changed continuously. Check this simple tutorial on how to display a label:

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

Then later you can animate it by using an NSTimer to call a method updating the labels x coordinate continuously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM