前幾天做新版本遇到需要文本滾動的需求,當(dāng)時考慮到以后可能會擴展和需求變動祝辣,所以當(dāng)時就參考了一下資料切油,自己封裝了一個簡單的,基本可以滿足日常使用澎胡,這幾天閑下來了就整理了一下,效果圖如下
使用很簡單稚伍,看一下.h文件
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger,MyTextScrollMode){
MyTextScrollContinuous, //連續(xù)的滾動
MyTextScrollSpace, //間隔的滾動
MyTextScrollRound //往復(fù)的滾動
};
typedef NS_ENUM(NSInteger,MyTextScrollDirection){
MyTextScrollMoveLeft, //向左滾動
MyTextScrollMoveRight //向右滾動
};
@interface MyScrollTextView : UIView
//滾動類型
@property (nonatomic) MyTextScrollMode textScrollMode;
//滾動方向
@property (nonatomic) MyTextScrollDirection textScrollDirection;
//字體顏色
@property (nonatomic,strong) UIColor * textColor;
//文字內(nèi)容
@property (nonatomic,strong) NSString * text;
//字體大小
@property (nonatomic,strong) UIFont * textFont;
//滾動速度 不設(shè)置就默認(rèn)
@property (nonatomic)CGFloat speed;
//連續(xù)滾動時兩段之間間隔大小
@property (nonatomic)CGFloat disance;
//開始滾動
- (void)startScroll;
@end
如演示圖所示的个曙,有三種模式可以設(shè)置,只有當(dāng)文字長度超過ScrollTextView的寬度時垦搬,文字才會滾動,使用方法如下:
//設(shè)置連續(xù)滾動
self.ContinuousScrollTextView.textScrollMode = MyTextScrollContinuous;
self.ContinuousScrollTextView.textScrollDirection = MyTextScrollMoveLeft;
self.ContinuousScrollTextView.textColor = [UIColor orangeColor];
self.ContinuousScrollTextView.textFont = [UIFont systemFontOfSize:17.f];
self.ContinuousScrollTextView.text = @"我是連續(xù)的滾動測試測試測試測試測試測試";
開始滾動調(diào)用方法:
[self.ContinuousScrollTextView startScroll];
具體的使用這里有一個小demo,有興趣可以看一下
點我進(jìn)入
大家覺得不錯給個star悼沿,謝謝
使用過程中有問題可以隨時探討,iOS小白慌植,錯誤的地方還望見諒义郑。