Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void)setupView {
- // Set background color to white
- [self setBackgroundColor:[UIColor clearColor]];
- // Set a corner radius
- //[self.layer setCornerRadius:5.0f];
- //[self.layer setBorderWidth:2.0f];
- //[self.layer setBorderColor:[UIColor blackColor].CGColor];
- //[self setClipsToBounds:YES];
- // Set the font
- tickerFont = [UIFont fontWithName:kTickerFontFamily size:kTickerFontSize];
- // Add the label (i'm gonna center it on the view - please feel free to do your own thing)
- tickerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height / 4, self.frame.size.width, self.frame.size.height)];
- [tickerLabel setBackgroundColor:[UIColor clearColor]];
- [tickerLabel setNumberOfLines:1];
- [tickerLabel setFont:tickerFont];
- [tickerLabel setTextColor:[UIColor whiteColor]];
- [self addSubview:tickerLabel];
- // Set that it loops by default
- loops = YES;
- }
- -(void)animateCurrentTickerString
- {
- NSString *currentString = [tickerStrings objectAtIndex:currentIndex];
- // Calculate the size of the text and update the frame size of the ticker label
- CGSize textSize = [currentString sizeWithFont:tickerFont constrainedToSize:CGSizeMake(9999, self.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
- // Move off screen
- [tickerLabel setFrame:CGRectMake(self.frame.size.width, tickerLabel.frame.origin.y, textSize.width, textSize.height)];
- // Set the string
- [tickerLabel setText:currentString];
- // Calculate a uniform duration for the item
- float duration = (textSize.width + self.frame.size.width) / tickerSpeed;
- // Create a UIView animation
- [UIView beginAnimations:@"" context:nil];
- [UIView setAnimationCurve:UIViewAnimationCurveLinear];
- [UIView setAnimationDuration:duration];
- [UIView setAnimationDelegate:self];
- [UIView setAnimationDidStopSelector:@selector(tickerMoveAnimationDidStop:finished:context:)];
- [tickerLabel setFrame:CGRectMake(-textSize.width, tickerLabel.frame.origin.y, textSize.width, textSize.height)];
- [UIView commitAnimations];
- }
- -(void)tickerMoveAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
- {
- // Update the index
- currentIndex++;
- // Check the index count
- if(currentIndex >= [tickerStrings count]) {
- currentIndex = 0;
- // Check if we should loop
- if(!loops) {
- // Set not running
- running = NO;
- return;
- }
- }
- // Animate
- [self animateCurrentTickerString];
- }
- #pragma mark - Ticker Animation Handling
- -(void)start {
- // Set the index to 0 on starting
- currentIndex = 0;
- // Set running
- running = YES;
- // Start the animation
- [self animateCurrentTickerString];
- }
Add Comment
Please, Sign In to add comment