使用Core Animation帮助进行股票代码滚动样式滚动

时间:2010-03-09 14:10:15

标签: objective-c cocoa core-animation

我正在寻找一些关于在OSX上实现核心动画中CALayers从右到左滚动的最佳方式的指导。我对Cocoa很陌生,不知道实现它的最佳方法。

我有连续的新闻项目和股票详细信息,我将其变成CALayers(由1张图片和一张CATextLayer组成),我想从自定义视图的右侧到左侧为CALayers设置动画。

新闻和股票信息不断更新,所以我想一次向视图添加1个项目,从右向左滚动,直到CALayer的最右侧点显示,然后将另一个CALayer添加到视图中并开始滚动它。我想做这个动态更新,而不是拍摄我所有数据的大快照,把它变成一个大的水平CALayer并滚动它。

我正在寻找如何实现这种效果的指导 -

我是否沿着视图中的路径手动为每个新CALayer设置动画?或者我应该使用CAScrollLayer来实现这种效果吗?

非常感谢 格伦。

2 个答案:

答案 0 :(得分:2)

我会使用Quartz Composer执行此操作,并在您的应用中添加QCView。这比你想象的要少得多。

答案 1 :(得分:2)

您可能不需要自定义任何内容来执行此操作。这里self是扩展UIScrollView的类的对象,并带有UILabel来显示文本。

////
//Header

#import <UIKit/UIKit.h>


@interface TickerScrollView : UIScrollView

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

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

@end

//////
//Implementation

@implementation TickerScrollView

@synthesize textLabel;

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

  // Initialization code
  [self setFrame: frame];

  [self setBounces: NO];

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

  [self setBackgroundColor:[UIColor greenColor]];

  [self initialiseTextLabel];

    }

    return self;

}

- (void)initialiseTextLabel {

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

 [self addSubview:textLabel];

 [self setScrollEnabled:YES];

}

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

 [self clearTicker];

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

 [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)/40;

  [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];
}


@end