//Header file
#import <UIKit/UIKit.h>
@class StockTiker;
@protocol StockTickerDelegate
-(UIView*)viewForRow:(int)row inTicker:(StockTiker*)stock_Ticker;
-(int)numberOfRowsForStockTiker:(StockTiker*)stock_Ticker;
@end
@interface StockTiker : UIView
{
int numberOfRows;
id <StockTickerDelegate>delegate;
}
@property (nonatomic, retain) id <StockTickerDelegate> delegate;
@property (nonatomic,assign)NSInteger tag;
@end
//Implementation File....
#import "StockTiker.h"
#import "QuartzCore/QuartzCore.h"
static int count=0;
@implementation StockTiker
@synthesize delegate,tag;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
numberOfRows=0;
// Initialization code
}
return self;
}
-(void)moveSubView
{
UIView *subView=[self.delegate viewForRow:count inTicker:self];
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
theAnimation.delegate=self;
theAnimation.removedOnCompletion=YES;
theAnimation.duration=10;
theAnimation.repeatCount=1;
theAnimation.autoreverses=NO;
theAnimation.fromValue=[NSNumber numberWithFloat:320];
theAnimation.toValue=[NSNumber numberWithFloat:-200];
[subView.layer addAnimation:theAnimation forKey:@"animateLayer"];
}
- (CGRect)visibleRect
{
CGRect visibleRect;
UIView *view=[self.delegate viewForRow:count inTicker:self];
visibleRect.origin = [view.layer.presentationLayer frame].origin;
visibleRect.size = view.frame.size;
return visibleRect;
}
-(void)addElement:(UIView*)subView
{
if (![self.subviews containsObject:(id)subView]) {
[self addSubview:subView];
[self moveSubView];
}
}
-(void)checkPosition
{
float x=[self visibleRect].origin.x+[self visibleRect].size.width;
if (x<300) {
count=count+1;
if (count==numberOfRows) {
count=0;
}
}
UIView *subView=[self.delegate viewForRow:count inTicker:self];
[self addElement:subView];
}
- (void)drawRect:(CGRect)rect
{
numberOfRows=[self.delegate numberOfRowsForStockTiker:self];
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(checkPosition) userInfo:nil repeats:YES];
}
#pragma mark- animation did stop selector
- (void)animationDidStop:(CABasicAnimation *)theAnimation finished:(BOOL)flag
{
[[self.subviews objectAtIndex:0] removeFromSuperview];
}
@end