本文主要介紹 【當UILabel的內容超出其自身的寬度範圍後,進行互動展示的效果】,我們先來看一下Demo的效果圖。
實際實現起來並不十分繁雜,在這裏,爲了開發的效率,我們使用了一個已經封裝好的UILabel控制類CBAutoScrollLabel:點擊“閱讀原文”下載。
在寫代碼之前,我們還有一個準備工作,在stroyBoard中,找到需要實現效果的viewController,並向需要展示滾動效果label的地方拖一個UIView空間,並將其大小確定好(範圍和之前需要展示滾動效果的label相同),之後將這個UIView的Class填寫爲CBAutoScrollLabel,如下圖:
如果是純代碼寫的界面,同理操作即可。
下面是主要的代碼示例:
在需要實現效果的controller中,將剛纔拖好的UIView進行關聯。
@property (weak, nonatomic) IBOutlet CBAutoScrollLabel *autoScrollLabel;
然後我們在viewDidLoad中
self.autoScrollLabel.text = @"測試label";
self.autoScrollLabel.layer.masksToBounds = YES;
self.autoScrollLabel.layer.cornerRadius = 4;
self.autoScrollLabel.textColor = kColorView;
self.autoScrollLabel.backgroundColor = [UIColor whiteColor];
self.autoScrollLabel.font = [UIFont systemFontOfSize:12];
// 開始和結束標籤之間的距離
self.autoScrollLabel.labelSpacing = 30;
// 一秒的停頓之後再開始滾動
self.autoScrollLabel.pauseInterval = 1.7;
// 每秒像素
self.autoScrollLabel.scrollSpeed = 30;
// 不使用自動滾動時的中心文本
self.autoScrollLabel.textAlignment = NSTextAlignmentCenter;
self.autoScrollLabel.fadeLength = 12.f;
self.autoScrollLabel.scrollDirection = CBAutoScrollDirectionLeft;
[self.autoScrollLabel observeApplicationNotifications];
這樣就基本完成了,如果有不同需求的效果可以自行進行調整。