2010-07-02 99 views
1

我只想在我的應用程序中有一個代碼, 我不知道要實現代碼請告訴我。如何在我的應用程序中實現代碼在iphone

感謝

+1

您可能需要確定__A ticker__多一點對我們幫助你;) – deanWombourne 2010-07-02 12:15:52

+0

誰投票決定關閉這個問題應該由現在已經學會這是你要求海報澄清的那種問題,而不是你過於模糊的那種問題。 – 2010-07-02 14:25:15

+0

可能的重複[有沒有人有一個很好的方法來滾動文字到一邊,就像在iPhone應用程序中的筆尖上的標籤上的股票報價?](http://stackoverflow.com/questions/900425/does-任何人都有一個很好的方式滾動文本關閉到一邊就像一個股票股票代碼我) – 2010-07-02 14:32:52

回答

1

的「北京時間」假設你的意思是水平滾動的文字:

一個股票基本上是由具有其x座標連續改變移動文本字符串。檢查如何顯示在標籤上這個簡單的教程:

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

後來的後來,你可以通過使用一個NSTimer調用更新標籤X座標連續的方法制作動畫。

+0

否 - 使用UIView類來控制動畫。這些都在Apple文檔中。 – SK9 2010-12-07 07:06:08

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 
相關問題