Skip to content

Instantly share code, notes, and snippets.

@osmszk
Created April 2, 2014 14:50
Show Gist options
  • Save osmszk/9935746 to your computer and use it in GitHub Desktop.
Save osmszk/9935746 to your computer and use it in GitHub Desktop.
Sliding Animation of UILabel like "Telop" or news texts on Social Game  スライドインするテロップ的なアニメーション
- (void)viewDidLoad
{
_newsLabel.text = @"abcdefgh.......";
[self startSlideNewsAnimation];
}
- (void)startSlideNewsAnimation
{
NSString *news = _newsLabel.text;
UIFont *font = [UIFont boldSystemFontOfSize:FONT_SIZE_L];
NSDictionary *attribute = @{NSFontAttributeName:font};
CGSize size;
if([SNUtil osVersion]>=7.0f){
size = [news boundingRectWithSize:CGSizeMake(640, 20)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:attribute
context:nil].size;
}else{
size = [news sizeWithFont:font constrainedToSize:CGSizeMake(640.0, 20.0) lineBreakMode:NSLineBreakByWordWrapping];
}
CGRect newRect = _newsLabel.frame;
newRect.size.width = size.width;
_newsLabel.frame = newRect;
static int MAX_LENGTH = 17;
static float SEC_PER_CHAR = 0.9f;
CGFloat sec = news.length * SEC_PER_CHAR;
if(sec < MAX_LENGTH*SEC_PER_CHAR){
sec = MAX_LENGTH*SEC_PER_CHAR;
}
[UIView animateWithDuration:sec
delay:0.5f
options:UIViewAnimationOptionCurveLinear
animations:^{
CGRect frame = _newsLabel.frame;
frame.origin.x = frame.origin.x - frame.size.width;
_newsLabel.frame = frame;
} completion:^(BOOL finished) {
[self startSlideNewsToStartPointAnimation];
}];
}
- (void)startSlideNewsToStartPointAnimation
{
CGRect newRect = _newsLabel.frame;
newRect.origin.x = 320;
_newsLabel.frame = newRect;
[UIView animateWithDuration:0.8f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^{
CGRect frame = _newsLabel.frame;
frame.origin.x = 128.0f;
_newsLabel.frame = frame;
} completion:^(BOOL finished) {
DEBUGLOG(@"second-slide Complete!!");
[self startSlideNewsAnimation];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment