代码之家  ›  专栏  ›  技术社区  ›  Arun Sharma

如何在iphone应用程序中实现Ticker

  •  1
  • Arun Sharma  · 技术社区  · 14 年前

    我只想在我的申请表上写个股票代码, 我不知道如何实现自动售票机请告诉我。

    谢谢

    2 回复  |  直到 14 年前
        1
  •  1
  •   sinsro    14 年前

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

    股票代码基本上只是一个文本字符串,它通过不断改变x坐标来移动。查看以下有关如何显示标签的简单教程:

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

    然后,您可以使用NSTimer调用一个方法来连续更新标签x坐标,从而为其设置动画。

        2
  •  5
  •   Ken    13 年前

    #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