iOS中UILabel滚动字幕动画的实现
2013-04-24 16:04
323 查看
有时候会遇到UILabel中的内容超出长度,显示不完全的问题。有一种解决方法是通过动画字幕来实现,比如:
字幕向左或者右滚动
字幕来回滚动
本文以后者为例来说明吧。这里先介绍UIView的通过Block实现的Animation以及其参数控制,最后是实现滚动字幕的代码。
UIView有方便的动画实现方式,SDK4.0以上,提供了三个Block的动画方式:
其中第一个最全面,可以设置UIViewAnimationOptions来控制动画的参数,比如重复,自动reverse之类的。
UIViewAnimationOptions具体定义如下:
本文实现方法就是使用Animation with Block的方式来实现UILabel来回滚动。代码如下:
字幕向左或者右滚动
字幕来回滚动
本文以后者为例来说明吧。这里先介绍UIView的通过Block实现的Animation以及其参数控制,最后是实现滚动字幕的代码。
UIView有方便的动画实现方式,SDK4.0以上,提供了三个Block的动画方式:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0 + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_0); // delay = 0.0, options = 0, completion = NULL
其中第一个最全面,可以设置UIViewAnimationOptions来控制动画的参数,比如重复,自动reverse之类的。
UIViewAnimationOptions具体定义如下:
enum { UIViewAnimationOptionLayoutSubviews = 1 << 0, UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only) UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default UIViewAnimationOptionCurveEaseIn = 1 << 16, UIViewAnimationOptionCurveEaseOut = 2 << 16, UIViewAnimationOptionCurveLinear = 3 << 16, UIViewAnimationOptionTransitionNone = 0 << 20, // default UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20, UIViewAnimationOptionTransitionFlipFromRight = 2 << 20, UIViewAnimationOptionTransitionCurlUp = 3 << 20, UIViewAnimationOptionTransitionCurlDown = 4 << 20, UIViewAnimationOptionTransitionCrossDissolve = 5 << 20, UIViewAnimationOptionTransitionFlipFromTop = 6 << 20, UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20, }; typedef NSUInteger UIViewAnimationOptions;
本文实现方法就是使用Animation with Block的方式来实现UILabel来回滚动。代码如下:
-(void)startAnimationIfNeeded{ //取消、停止所有的动画 [self.aUILabel.layer removeAllAnimations]; CGSize textSize = [self.aUILabel.text sizeWithFont:self.aUILabel.font]; CGRect lframe = self.aUILabel.frame; lframe.size.width = textSize.width; self.aUILabel.frame = lframe; const float oriWidth = 180; if (textSize.width > oriWidth) { float offset = textSize.width - oriWidth; [UIView animateWithDuration:3.0 delay:0 options:UIViewAnimationOptionRepeat //动画重复的主开关 |UIViewAnimationOptionAutoreverse //动画重复自动反向,需要和上面这个一起用 |UIViewAnimationOptionCurveLinear //动画的时间曲线,滚动字幕线性比较合理 animations:^{ self.aUILabel.transform = CGAffineTransformMakeTranslation(-offset, 0); } completion:^(BOOL finished) { } ]; } }
相关文章推荐
- iOS中UILabel滚动字幕动画的实现
- iOS中UILabel滚动字幕动画的实现
- iOS中UILabel滚动字幕动画的实现
- iOS实现滚动字幕的动画特效
- 使用UILabel实现滚动字幕移动效果
- IOS_实现tableview向上滚动全屏效果与点击展开动画
- UILabel滚动字幕的实现
- c#之滚动字幕动画窗体的实现详解
- iOS开发 实现动画的暂停和继续播放
- iOS 滚动数字控件:DPScrollNumberLabel 实现
- iOS 使用masonry布局,并实现展示动画
- iphone ios uiview uilabel 边框,圆角,阴影实现
- iOS --- 使用UIImageView来实现倒计时动画
- 使用DialogFragment实现仿ios正在加载动画效果
- iOS中UICollectionView实现单个cell滚动翻页并放大cell
- IOS跑马灯效果,实现文字水平无间断滚动
- iOS开发学习之动画实现
- jquery实现动画菜单的左右滚动、渐变及图形背景滚动等效果
- jquery实现动画菜单的左右滚动、渐变及图形背景滚动等效果
- 在iOS利用动画实现全景视图