Guest User

Untitled

a guest
Jul 21st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void)setupView {
  2.     // Set background color to white
  3.     [self setBackgroundColor:[UIColor clearColor]];
  4.    
  5.     // Set a corner radius
  6.     //[self.layer setCornerRadius:5.0f];
  7.     //[self.layer setBorderWidth:2.0f];
  8.     //[self.layer setBorderColor:[UIColor blackColor].CGColor];
  9.     //[self setClipsToBounds:YES];
  10.    
  11.     // Set the font
  12.     tickerFont = [UIFont fontWithName:kTickerFontFamily size:kTickerFontSize];
  13.    
  14.     // Add the label (i'm gonna center it on the view - please feel free to do your own thing)
  15.     tickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height / 4, self.frame.size.width, self.frame.size.height)];
  16.     [tickerLabel setBackgroundColor:[UIColor clearColor]];
  17.     [tickerLabel setNumberOfLines:1];
  18.     [tickerLabel setFont:tickerFont];
  19.     [tickerLabel setTextColor:[UIColor whiteColor]];
  20.    
  21.     [self addSubview:tickerLabel];
  22.    
  23.     // Set that it loops by default
  24.     loops = YES;
  25. }
  26.  
  27. -(void)animateCurrentTickerString
  28. {
  29.     NSString *currentString = [tickerStrings objectAtIndex:currentIndex];
  30.    
  31.     // Calculate the size of the text and update the frame size of the ticker label
  32.     CGSize textSize = [currentString sizeWithFont:tickerFont constrainedToSize:CGSizeMake(9999, self.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
  33.    
  34.     // Move off screen
  35.     [tickerLabel setFrame:CGRectMake(self.frame.size.width, tickerLabel.frame.origin.y, textSize.width, textSize.height)];
  36.    
  37.     // Set the string
  38.     [tickerLabel setText:currentString];
  39.        
  40.     // Calculate a uniform duration for the item   
  41.     float duration = (textSize.width + self.frame.size.width) / tickerSpeed;
  42.    
  43.     // Create a UIView animation
  44.     [UIView beginAnimations:@"" context:nil];
  45.     [UIView setAnimationCurve:UIViewAnimationCurveLinear];
  46.     [UIView setAnimationDuration:duration];
  47.     [UIView setAnimationDelegate:self];
  48.     [UIView setAnimationDidStopSelector:@selector(tickerMoveAnimationDidStop:finished:context:)];
  49.    
  50.     [tickerLabel setFrame:CGRectMake(-textSize.width, tickerLabel.frame.origin.y, textSize.width, textSize.height)];
  51.    
  52.     [UIView commitAnimations];
  53. }
  54.  
  55. -(void)tickerMoveAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
  56. {
  57.     // Update the index
  58.     currentIndex++;
  59.    
  60.     // Check the index count
  61.     if(currentIndex >= [tickerStrings count]) {
  62.         currentIndex = 0;
  63.  
  64.         // Check if we should loop
  65.         if(!loops) {
  66.             // Set not running
  67.             running = NO;
  68.        
  69.             return;
  70.         }
  71.     }
  72.    
  73.     // Animate
  74.     [self animateCurrentTickerString];
  75. }
  76.  
  77. #pragma mark - Ticker Animation Handling
  78. -(void)start {
  79.    
  80.     // Set the index to 0 on starting
  81.     currentIndex = 0;
  82.    
  83.     // Set running
  84.     running = YES;
  85.    
  86.     // Start the animation
  87.     [self animateCurrentTickerString];
  88. }
Add Comment
Please, Sign In to add comment