From d5bb1ed8f0103acf205bd64e4bd831d1809fbdbb Mon Sep 17 00:00:00 2001 From: zgy <173678978@qq.com> Date: Wed, 15 Apr 2020 21:01:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D[#16](https://github.com/malt?= =?UTF-8?q?sugar/RollingNotice/issues/16)=E3=80=81[#17](https://github.com?= =?UTF-8?q?/maltsugar/RollingNotice/issues/17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + RollingNotice-Swift.podspec | 2 +- .../GYRollingNoticeView.swift | 52 +++++++++++++------ 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 93830db..2fd7b79 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ #### (for Swfit 4.2+) ### 版本记录 +- 1.1.0 修复[#16](https://github.com/maltsugar/RollingNotice/issues/16)、[#17](https://github.com/maltsugar/RollingNotice/issues/17),感谢[@xygkevin][https://github.com/xygkevin] - 1.0.9 新增暂停/恢复功能 - 1.0.8 修复[#4](https://github.com/maltsugar/RollingNotice-Swift/issues/4)数据源返回数量为0时,不再显示cell,感谢建议 - 1.0.7 普通用法textLabel支持左右间距设置 diff --git a/RollingNotice-Swift.podspec b/RollingNotice-Swift.podspec index a0ee13b..26f4af4 100644 --- a/RollingNotice-Swift.podspec +++ b/RollingNotice-Swift.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "RollingNotice-Swift" - s.version = "1.0.9" + s.version = "1.1.0" s.summary = "滚动公告、广告,支持灵活自定义cell。淘宝、口碑、京东、美团、天猫等等一切滚动广告 Roll Notice or Advertising. A rolling banner, custom cell like UITableViewCell supported" # This description is used to generate tags and improve search results. diff --git a/RollingNotice-Swift/GYRollingNoticeView-Swift/GYRollingNoticeView.swift b/RollingNotice-Swift/GYRollingNoticeView-Swift/GYRollingNoticeView.swift index 144fb5c..d7a7f9d 100644 --- a/RollingNotice-Swift/GYRollingNoticeView-Swift/GYRollingNoticeView.swift +++ b/RollingNotice-Swift/GYRollingNoticeView-Swift/GYRollingNoticeView.swift @@ -24,8 +24,20 @@ open class GYRollingNoticeView: UIView { weak open var dataSource : GYRollingNoticeViewDataSource? weak open var delegate : GYRollingNoticeViewDelegate? open var stayInterval = 2.0 - open private(set) var currentIndex = 0 open private(set) var status: GYRollingNoticeViewStatus = .idle + open var currentIndex: Int { + guard let count = (self.dataSource?.numberOfRowsFor(roolingView: self)) else { return 0} + + if (_cIdx > count - 1) { + _cIdx = 0 + } + return _cIdx; + } + + + private var _cIdx = 0 + private var _needTryRoll = false + // MARK: private properties private lazy var cellClsDict: Dictionary = { () -> [String : Any] in @@ -110,7 +122,7 @@ open class GYRollingNoticeView: UIView { status = .idle isAnimating = false - currentIndex = 0 + _cIdx = 0 currentCell?.removeFromSuperview() willShowCell?.removeFromSuperview() currentCell = nil @@ -145,6 +157,13 @@ open class GYRollingNoticeView: UIView { override open func touchesBegan(_ touches: Set, with event: UIEvent?) { } + open override func layoutSubviews() { + super.layoutSubviews() + if (_needTryRoll) { + self.reloadDataAndStartRoll() + _needTryRoll = false + } + } } @@ -156,7 +175,7 @@ extension GYRollingNoticeView{ return } layoutCurrentCellAndWillShowCell() - currentIndex += 1 + let w = self.frame.size.width let h = self.frame.size.height @@ -172,6 +191,7 @@ extension GYRollingNoticeView{ self.currentCell = cell1 } self.isAnimating = false + self._cIdx += 1 } } @@ -179,25 +199,30 @@ extension GYRollingNoticeView{ fileprivate func layoutCurrentCellAndWillShowCell() { guard let count = (self.dataSource?.numberOfRowsFor(roolingView: self)) else { return } - if (currentIndex > count - 1) { - currentIndex = 0 + if (_cIdx > count - 1) { + _cIdx = 0 } - var willShowIndex = currentIndex + 1 + var willShowIndex = _cIdx + 1 if (willShowIndex > count - 1) { willShowIndex = 0 } - // print(">>>>%d", currentIndex) + // print(">>>>%d", _cIdx) let w = self.frame.size.width let h = self.frame.size.height -// print("count: \(count), currentIndex:\(currentIndex) willShowIndex: \(willShowIndex)") +// print("count: \(count), _cIdx:\(_cIdx) willShowIndex: \(willShowIndex)") + + if !(w > 0 && h > 0) { + _needTryRoll = true + return + } if currentCell == nil { // 第一次没有currentcell // currentcell is null at first time - if let cell = self.dataSource?.rollingNoticeView(roolingView: self, cellAtIndex: currentIndex) { + if let cell = self.dataSource?.rollingNoticeView(roolingView: self, cellAtIndex: _cIdx) { currentCell = cell cell.frame = CGRect(x: 0, y: 0, width: w, height: h) self.addSubview(cell) @@ -237,14 +262,7 @@ extension GYRollingNoticeView{ } @objc fileprivate func handleCellTapAction(){ - guard let count = self.dataSource?.numberOfRowsFor(roolingView: self) else { - return - } - - if (currentIndex > count - 1) { - currentIndex = 0; - } - self.delegate?.rollingNoticeView?(self, didClickAt: currentIndex) + self.delegate?.rollingNoticeView?(self, didClickAt: self.currentIndex) } fileprivate func setupNoticeViews() {