iOS中UILabel滾動(dòng)字幕動(dòng)畫(huà)的實(shí)現(xiàn)

有時(shí)候會(huì)遇到UILabel中的內(nèi)容超出長(zhǎng)度,顯示不完全的問(wèn)題。有一種解決方法是通過(guò)動(dòng)畫(huà)字幕來(lái)實(shí)現(xiàn),比如:

1.字幕向左或者右滾動(dòng)

2.字幕來(lái)回滾動(dòng)

本文以后者為例來(lái)說(shuō)明吧。這里先介紹UIView的通過(guò)Block實(shí)現(xiàn)的Animation以及其參數(shù)控制,最后是實(shí)現(xiàn)滾動(dòng)字幕的代碼。

1. UIView有方便的動(dòng)畫(huà)實(shí)現(xiàn)方式,SDK4.0以上,提供了三個(gè)Block的動(dòng)畫(huà)方式:

+ (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

其中第一個(gè)最全面,可以設(shè)置UIViewAnimationOptions來(lái)控制動(dòng)畫(huà)的參數(shù),比如重復(fù),自動(dòng)reverse之類(lèi)的。

2. 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;

3. 本文實(shí)現(xiàn)方法就是使用Animation with Block的方式來(lái)實(shí)現(xiàn)UILabel來(lái)回滾動(dòng)。代碼如下:

-(void)startAnimationIfNeeded{

//取消、停止所有的動(dòng)畫(huà)

[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 //動(dòng)畫(huà)重復(fù)的主開(kāi)關(guān)

|UIViewAnimationOptionAutoreverse //動(dòng)畫(huà)重復(fù)自動(dòng)反向,需要和上面這個(gè)一起用

|UIViewAnimationOptionCurveLinear //動(dòng)畫(huà)的時(shí)間曲線(xiàn),滾動(dòng)字幕線(xiàn)性比較合理

animations:^{

self.aUILabel.transform = CGAffineTransformMakeTranslation(-offset, 0);

}

completion:^(BOOL finished) {

}

];

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容