優(yōu)雅地實現(xiàn)上下跑馬燈效果

設(shè)計思路:

  1. 創(chuàng)建兩個UIButton,主要是利用 button, 有backgroundImageimage潭苞, titleLabel 屬性肃拜,可以方便的設(shè)置文字和圖片痴腌,如果想用UILabel,直接替換即可燃领;
  2. 使用Timer計時器士聪,切換數(shù)據(jù)用;
  3. 設(shè)置默認值柿菩,如果集合中沒有數(shù)據(jù)戚嗅,給一個默認的,并且不讓觸發(fā)計時器;
  4. 更新兩個Buttonframe懦胞,來回切換替久;
  5. titleLabel 的展示,給當(dāng)前currentButton設(shè)置新值躏尉,給另一個firstButton設(shè)置currentButton. titleLabel .text的值

然后直接上代碼蚯根,如果疑問,請留言胀糜。

struct BLConstStruct {
    var currentRect: CGRect = CGRect.zero
    var lastRect: CGRect = CGRect.zero
    var count = 1
}

class BLScrollLabelItem: NSObject {
    
    var font: UIFont = BLFont(14)
    var color: UIColor = BLDarkColor
    var imageName: String = ""
}


class BLScrollLabel: UIView {
    
    var dataArray: Array<String>? {
        
        didSet {
            dealLogicMetod()
        }
    }
    
    private lazy var changeTimer: Timer! = {
        
        let timer = Timer.init(timeInterval: 3.0, target: self, selector: #selector(timeCounterMethod), userInfo: nil, repeats: true)
        
        RunLoop.current.add(timer, forMode: RunLoop.Mode.common)
        return timer;
    }()
    
    
    private var constSize: BLConstStruct!
    private var item: BLScrollLabelItem?
    
    
    private lazy var firstButton: UIButton = {
        let button = BLButton(title: "", font: self.item?.font, image: self.item?.imageName, selectedImg: nil, toView: self)
        
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
        button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        button.addTarget(self, action: #selector(scrollLabelEvent(sender:)), for: .touchUpInside)
        
        return button
    }()
    
    private lazy var currentButton: UIButton = {
        let button = BLButton(title: "", font: self.item?.font, image: self.item?.imageName, selectedImg: nil, toView: self)
        
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        button.titleLabel?.lineBreakMode = NSLineBreakMode.byTruncatingTail
        button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
        button.addTarget(self, action: #selector(scrollLabelEvent(sender:)), for: .touchUpInside)
        
        return button
    }()
    
    init(item: BLScrollLabelItem, size: CGSize) {
       
        let frame = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
        super.init(frame: frame)
        
        let currentRect = CGRect(x: 10, y: 0, width: size.width-20, height: size.height)
       
        let lastRect = CGRect(x: 10, y: size.height, width: size.width-20, height: size.height)

        constSize = BLConstStruct(currentRect: currentRect, lastRect: lastRect, count: 0)

        self.item = item
        self.clipsToBounds = true;
        self.backgroundColor = UIColor.green;
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func dealLogicMetod() {
        
        firstButton.frame = constSize.currentRect
        firstButton.titleLabel?.font = item?.font
        firstButton.setTitleColor(item?.color, for: .normal)
        firstButton.setImage(BLImageNamed(item?.imageName), for: .normal)
        
        
        guard (dataArray!.isEmpty == false) else {
            firstButton.setTitle("暫無資訊", for: .normal)
            firstButton.isUserInteractionEnabled = false
            return
        }
        
        let model = dataArray!.first
        firstButton.setTitle(model, for: .normal)
        
        currentButton.frame = constSize.lastRect;
        currentButton.titleLabel?.font = item?.font
        currentButton.setTitleColor(item?.color, for: .normal)
        currentButton.setImage(BLImageNamed(item?.imageName), for: .normal)
        
        guard changeTimer.isValid else { return }
        changeTimer.fire()
    }
    
    @objc private func timeCounterMethod() {
        
        if constSize.count < (dataArray!.count-1)
        {
            constSize.count += 1
        } else
        {
            constSize.count = 0
        }
        //由當(dāng)前顯示颅拦,漸變到 -self.letSize.height,
        firstButton.frame = constSize.currentRect;
        firstButton.alpha = 8.0
        
        //由底部教藻,漸變到當(dāng)前顯示
  
        currentButton.frame = constSize.lastRect;
        currentButton.alpha = 0.3
        
        let model = dataArray![constSize.count]
        currentButton.setTitle(model, for: .normal)
        
        
        UIView.animate(withDuration: 1.0, animations: {
            
            var frame = self.constSize.currentRect
            frame.origin.y = -frame.height
            self.firstButton.frame = frame;
            self.firstButton.alpha = 0.3
            
            self.currentButton.frame = self.constSize.currentRect;
            self.currentButton.alpha = 0.6
            
        }) { (make) in
            
            self.currentButton.alpha = 1.0
            self.firstButton.alpha = 0.0
            self.firstButton.setTitle(self.currentButton.titleLabel?.text, for: .normal)
        }
    }
    
    @objc private func scrollLabelEvent(sender: UIButton) {
        
    }
    
    deinit {
        if changeTimer.isValid {
            changeTimer.invalidate()
            changeTimer = nil
        }
    }
}

使用

import UIKit

class BLViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let item: BLScrollLabelItem = BLScrollLabelItem()
        item.color = UIColor.darkGray
        item.font = BLFont(17)
        item.imageName = "radio-broadcast"
        
        let frame = CGRect(x: 0, y: 100, width: kScreen_width, height: 50)
        
        let scrollLabel: BLScrollLabel = BLScrollLabel.init(item: item, size: frame.size)
        scrollLabel.frame.origin = frame.origin
        scrollLabel.dataArray = ["首頁接口   熱門課程——老接口距帅,近期直", "業(yè)講師—講師列表接口,資訊——資訊接口", "熱門課程—"]
        self.view.addSubview(scrollLabel)
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末括堤,一起剝皮案震驚了整個濱河市碌秸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌悄窃,老刑警劉巖讥电,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異轧抗,居然都是意外死亡恩敌,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門横媚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纠炮,“玉大人,你說我怎么就攤上這事分唾】古觯” “怎么了?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵绽乔,是天一觀的道長弧蝇。 經(jīng)常有香客問我,道長折砸,這世上最難降的妖魔是什么看疗? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮睦授,結(jié)果婚禮上两芳,老公的妹妹穿的比我還像新娘。我一直安慰自己去枷,他們只是感情好怖辆,可當(dāng)我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布是复。 她就那樣靜靜地躺著,像睡著了一般竖螃。 火紅的嫁衣襯著肌膚如雪淑廊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天特咆,我揣著相機與錄音季惩,去河邊找鬼。 笑死腻格,一個胖子當(dāng)著我的面吹牛画拾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播菜职,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼青抛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了些楣?” 一聲冷哼從身側(cè)響起脂凶,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宪睹,失蹤者是張志新(化名)和其女友劉穎愁茁,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體亭病,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡鹅很,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了罪帖。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片促煮。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖整袁,靈堂內(nèi)的尸體忽然破棺而出菠齿,到底是詐尸還是另有隱情,我是刑警寧澤坐昙,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布绳匀,位于F島的核電站,受9級特大地震影響炸客,放射性物質(zhì)發(fā)生泄漏疾棵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一痹仙、第九天 我趴在偏房一處隱蔽的房頂上張望是尔。 院中可真熱鬧,春花似錦开仰、人聲如沸拟枚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恩溅。三九已至痕囱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間暴匠,已是汗流浹背鞍恢。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留每窖,地道東北人帮掉。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像窒典,于是被迫代替她去往敵國和親蟆炊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,440評論 2 359