Advertisement
Guest User

Stock Ticker

a guest
Feb 1st, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Header file
  2.  
  3. #import <UIKit/UIKit.h>
  4.  
  5. @class StockTiker;
  6.  
  7. @protocol StockTickerDelegate
  8. -(UIView*)viewForRow:(int)row inTicker:(StockTiker*)stock_Ticker;
  9. -(int)numberOfRowsForStockTiker:(StockTiker*)stock_Ticker;
  10. @end
  11.  
  12. @interface StockTiker : UIView
  13. {
  14.     int numberOfRows;
  15.     id <StockTickerDelegate>delegate;
  16. }
  17. @property (nonatomic, retain) id <StockTickerDelegate> delegate;
  18. @property (nonatomic,assign)NSInteger tag;
  19.  
  20. @end
  21.  
  22.  
  23. //Implementation File....
  24.  
  25. #import "StockTiker.h"
  26. #import "QuartzCore/QuartzCore.h"
  27.  
  28. static int count=0;
  29.  
  30. @implementation StockTiker
  31. @synthesize delegate,tag;
  32.  
  33. - (id)initWithFrame:(CGRect)frame
  34. {
  35.     self = [super initWithFrame:frame];
  36.     if (self) {
  37.         numberOfRows=0;
  38.         // Initialization code
  39.     }
  40.     return self;
  41. }
  42.  
  43. -(void)moveSubView
  44. {
  45.     UIView *subView=[self.delegate viewForRow:count inTicker:self];
  46.    
  47.     CABasicAnimation *theAnimation;
  48.     theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
  49.     theAnimation.delegate=self;
  50.     theAnimation.removedOnCompletion=YES;
  51.     theAnimation.duration=10;
  52.     theAnimation.repeatCount=1;
  53.     theAnimation.autoreverses=NO;
  54.     theAnimation.fromValue=[NSNumber numberWithFloat:320];
  55.     theAnimation.toValue=[NSNumber numberWithFloat:-200];
  56.     [subView.layer addAnimation:theAnimation forKey:@"animateLayer"];
  57. }
  58.  
  59. - (CGRect)visibleRect
  60. {
  61.     CGRect visibleRect;
  62.     UIView *view=[self.delegate viewForRow:count inTicker:self];
  63.     visibleRect.origin = [view.layer.presentationLayer frame].origin;
  64.     visibleRect.size = view.frame.size;
  65.     return visibleRect;
  66. }
  67.  
  68. -(void)addElement:(UIView*)subView
  69. {
  70.     if (![self.subviews containsObject:(id)subView]) {
  71.         [self addSubview:subView];
  72.         [self moveSubView];
  73.     }
  74. }
  75.  
  76. -(void)checkPosition
  77. {
  78.     float x=[self visibleRect].origin.x+[self visibleRect].size.width;
  79.    
  80.     if (x<300) {
  81.         count=count+1;
  82.         if (count==numberOfRows) {
  83.             count=0;
  84.         }
  85.     }
  86.    
  87.     UIView *subView=[self.delegate viewForRow:count inTicker:self];
  88.     [self addElement:subView];
  89. }
  90.  
  91. - (void)drawRect:(CGRect)rect
  92. {    
  93.     numberOfRows=[self.delegate numberOfRowsForStockTiker:self];
  94.     [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(checkPosition) userInfo:nil repeats:YES];
  95. }
  96.  
  97. #pragma mark- animation did stop selector
  98.  
  99. - (void)animationDidStop:(CABasicAnimation *)theAnimation finished:(BOOL)flag
  100. {
  101.     [[self.subviews objectAtIndex:0] removeFromSuperview];
  102. }
  103. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement