思路借鑒demo,我索性改造成了swift版本的。
https://github.com/login?return_to=%2Fzhouxubin%2FXBLoopLabel
具體調(diào)用代碼:
class ThirdVC: UIViewController {
var dataSouece : [String] = ["111","3333","dasd"]
var loopV : TextLoopV!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loopV = TextLoopV.textLoopView(with: dataSouece, loopInterval: 3.0, initWithFrame: CGRect.init(x: 0, y: 100, width: 300, height: 100)) { (a, ind) in
prints(a,ind)
}
view.addSubview(loopV)
}
}
實現(xiàn)代碼:
class TextLoopV: UIView,UITableViewDelegate,UITableViewDataSource {
/// 定義閉包
typealias sendValueClosure = (_ sendString: String,_ index : Int) -> Void//聲明
/// 回調(diào)
var callBackString: sendValueClosure?//持有
/// 數(shù)據(jù)源
var dataSource : [String] = [String]()
/// 表格
var tableView : UITableView!
/// 累加索引
var currentRowIndex : Int = 0
/// 自動幾秒時間
var interval : TimeInterval {
get {
return 1.0
}
set {
let timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(timerStuff), userInfo: nil, repeats: true)
self.timer = timer
}
}
var timer : Timer!
/// 定時器做的事
@objc private func timerStuff() {
prints(self.currentRowIndex)
self.currentRowIndex += 1
self.tableView.setContentOffset(CGPoint.init(x: 0, y: CGFloat(self.currentRowIndex) * self.tableView.rowHeight), animated: true)
}
/// 初始化上下滾動的視圖
///
/// - Parameters:
/// - dataSource: 數(shù)據(jù)源
/// - timeInterval: 時間間隔
/// - frame: 容器大小
/// - selectBlock: 傳回的閉包
/// - Returns: 返回本身
class func textLoopView(with dataSource: [String], loopInterval timeInterval: TimeInterval, initWithFrame frame: CGRect, select selectBlock: @escaping sendValueClosure) -> TextLoopV {
let loopV = TextLoopV.init(frame: frame)
loopV.dataSource = dataSource
loopV.interval = timeInterval
loopV.callBackString = selectBlock
return loopV
}
override init(frame: CGRect) {
super.init(frame: frame)
self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: frame.width, height: frame.height))
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.separatorStyle = .none
self.tableView.layer.borderWidth = 1
self.tableView.rowHeight = frame.height
self.tableView.zdx_register(TextLoopCell.classForCoder())
self.addSubview(self.tableView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//MARK:scrollView代理
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
if self.currentRowIndex == self.dataSource.count {
self.currentRowIndex = 0
self.tableView.setContentOffset(CGPoint.zero, animated: false)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if self.callBackString != nil {
self.callBackString!(self.dataSource[indexPath.row],indexPath.row)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.zdx_dequeueReusableCell(indexPath: indexPath) as TextLoopCell
cell.ddd.text = self.dataSource[indexPath.row]
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
}
自定義的表格單元格cell:
class TextLoopCell: CommonTableViewCell {
var ddd : UILabel!
var baseIMg : BaseUIImageView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
ddd = UILabel.init(frame: frame)
baseIMg = BaseUIImageView.init(CGRect.init(x: frame.midX, y: frame.midY, width: frame.width / 2, height: frame.height / 2), #imageLiteral(resourceName: "default"))
contentView.addSubview(ddd)
contentView.addSubview(baseIMg)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
1.有幾個注意點:oc 定義變量后瞒津,會默認持有setter和getter方法卢未。
swift對應(yīng)的是set 匆骗、get卿捎。
http://www.reibang.com/p/87fc570e44df
2.再則,就是swift中閉包的定義注意视搏。
http://www.reibang.com/p/5a3fd872257e