通過UIView animate block 方式簡單實現
-(void)startAnimation{
? ? ? //取消所有的動畫
? ? ? //[self.aUILabel.layer removeAllAnimations];
? ? ?//計算實際text大小
? ? ?CGSizetextSize = ? ? ? ? ? ?[self.aUILabel.textsizeWithAttributes:@{NSFontAttributeName:self.aUILabel.font}];
? ? ?//保存label的frame值
? ? ?CGRectlframe =self.aUILabel.frame;
? ? ?//用計算出來的text的width更改frame的原始width
? ? lframe.size.width= textSize.width;
? ? //從屏幕最右邊向左邊移動
? ? lframe.origin.x=self.view.bounds.size.width;
? ?//用新值更改label的原frame值
? ?self.aUILabel.frame= lframe;
? ?//計算動畫x移動的最大偏移:屏幕width+text的width
? ?floatoffset = textSize.width+self.view.bounds.size.width;
? ?[UIView animateWithDuration:10.0
? ? ? ? ? ? ? ? delay:0
? ? ? ? ? ? ? ? options:UIViewAnimationOptionRepeat//動畫重復的主開關
? ? ? ? ? ? ? ? |UIViewAnimationOptionCurveLinear//動畫的時間曲線
? ? ? ? ? ? ? ? animations:^{
? ? ? ? ? ? ? ? ? ? self.aUILabel.transform=CGAffineTransformMakeTranslation(-offset,0);
}
?completion:^(BOOLfinished) {
}
? ? ?];
}
附圖片:
