美文网首页
Swift 实现导航栏文字的无限滚动

Swift 实现导航栏文字的无限滚动

作者: 平凡的心 | 来源:发表于2016-10-20 11:03 被阅读241次

    1.项目中引入第三方框架MarqueeLabel,地址如下:

    https://github.com/search?utf8=✓&q=MarqueeLabel&type=Repositories&ref=searchresults

    2.那上代码吧:

    import UIKit

    import MarqueeLabel_Swift

    class MarqueeTitleView: UIView {

    init(frame: CGRect, title: String) {

    super.init(frame: frame)

    let label = MarqueeLabel(frame: frame)

    label.text = title

    label.textColor = .whiteColor()

    label.font = UIFont.boldSystemFontOfSize(17)

    label.textAlignment = .Center

    label.scrollDuration = 10

    label.trailingBuffer = 30

    label.fadeLength = 10

    label.animationCurve = .EaseInOut

    self.addSubview(label)

    }

    required init?(coder aDecoder: NSCoder) {

    fatalError("init(coder:) has not been implemented")

    }

    }

    注:导入框架后自定义一个view将上面代码粘贴替换.

    3.控制器中代码:

    class BaseViewController: UIViewController {

    override func viewDidLoad() {

    super.viewDidLoad()

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "nav_back", target: self, action: #selector(back))

    let width = view.frame.width - 120

    let titleView = MarqueeTitleView(frame: CGRect(x: 0, y: 0, width: width, height: 30), title: "这个地方是你要导航栏上title滚动的文字")

    navigationItem.titleView = titleView

    }

    }

    4.好了,赶快去试试吧,惊喜等你发现.

    相关文章

      网友评论

          本文标题:Swift 实现导航栏文字的无限滚动

          本文链接:https://www.haomeiwen.com/subject/hwsfuttx.html