diff --git a/MRTableViewCellCountScrollIndicator.podspec b/MRTableViewCellCountScrollIndicator.podspec index db3f7e1..1bafb6a 100644 --- a/MRTableViewCellCountScrollIndicator.podspec +++ b/MRTableViewCellCountScrollIndicator.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'MRTableViewCellCountScrollIndicator' - s.version = '0.0.6' + s.version = '0.0.8' s.summary = 'Table View Cell Scroll Count Indicator' # This description is used to generate tags and improve search results. diff --git a/MRTableViewCellCountScrollIndicator/Classes/MRTableViewCellCountScrollIndicator.swift b/MRTableViewCellCountScrollIndicator/Classes/MRTableViewCellCountScrollIndicator.swift index 9c6501a..461553c 100644 --- a/MRTableViewCellCountScrollIndicator/Classes/MRTableViewCellCountScrollIndicator.swift +++ b/MRTableViewCellCountScrollIndicator/Classes/MRTableViewCellCountScrollIndicator.swift @@ -10,7 +10,8 @@ public class MRTableViewCellCountScrollIndicator:NSObject, UIScrollViewDelegate private var dragging:Bool = false private var dynamicUnpagedHeight:Bool = false var y:CGFloat = -1000 - private var topConstraint : NSLayoutConstraint! + private var topConstraint: NSLayoutConstraint! + var rectHeight:CGFloat = 0 public var opacity:CGFloat = 1 { didSet { @@ -31,6 +32,7 @@ public class MRTableViewCellCountScrollIndicator:NSObject, UIScrollViewDelegate public init(tableView:UITableView) { self.tableView = tableView tableView.layoutIfNeeded() + super.init() scrollCountView.translatesAutoresizingMaskIntoConstraints = false tableView.addSubview(scrollCountView) @@ -47,6 +49,7 @@ public class MRTableViewCellCountScrollIndicator:NSObject, UIScrollViewDelegate rightConstraint = NSLayoutConstraint(item: scrollCountView, attribute: .Trailing, relatedBy: .Equal, toItem: tableView, attribute: .Leading, multiplier: 1, constant: tableView.contentSize.width) tableView.addConstraint(rightConstraint) + self.rectHeight = self.tableView.frame.height showCellScrollCount(false) } @@ -68,24 +71,28 @@ public class MRTableViewCellCountScrollIndicator:NSObject, UIScrollViewDelegate } func updateScrollPosition() { - - //var currentCellRect:CGRect? - let indexPaths = tableView.indexPathsForVisibleRows + if totalScrollCountNum == 0 { + return + } + self.rectHeight = tableView.bounds.size.height var currentCellRect:CGRect? - if let indexPaths = indexPaths { - if indexPaths.count > 0 { - currentCellRect = tableView.rectForRowAtIndexPath(indexPaths[0]) - scrollCountView.currentScrollCountNum = indexPaths[0].row + var percentage: CGFloat = 0 + if (self.rectHeight != 0) { + percentage = (self.tableView.contentOffset.y)/(self.tableView.contentSize.height - self.rectHeight) + if (percentage < 0) { + percentage = 0 + } + if let index = self.tableView.indexPathForRowAtPoint(CGPoint(x: 0, y: self.tableView.contentOffset.y + percentage*self.rectHeight)) { + scrollCountView.currentScrollCountNum = index.row + currentCellRect = tableView.rectForRowAtIndexPath(index) } } - guard let currentCellRectu = currentCellRect else { return } if (dynamicUnpagedHeight) { - let viewSize = tableView.bounds.size.height let tableContentHeight = tableView.contentSize.height let scrollLimit = tableContentHeight - viewSize @@ -112,28 +119,23 @@ public class MRTableViewCellCountScrollIndicator:NSObject, UIScrollViewDelegate size: CGSize(width: width, height: scrollCountViewHeight) ) } else { - if totalScrollCountNum == 0 { - return - } - let oneDistance = tableView.bounds.size.height / CGFloat(totalScrollCountNum) let currentCellHeight = currentCellRectu.size.height let currentCellOffset = currentCellRectu.origin.y - let currentOffset = tableView.contentOffset.y - + var currentOffset = tableView.contentOffset.y + percentage*self.rectHeight let dxp = (currentOffset - currentCellOffset)/currentCellHeight - let dx = dxp * oneDistance - var finalY = tableView.contentOffset.y + ((tableView.bounds.size.height*CGFloat(scrollCountView.currentScrollCountNum)) / CGFloat(totalScrollCountNum)) + dx + let dx = dxp * oneDistance + var ratio = CGFloat(scrollCountView.currentScrollCountNum) / CGFloat(totalScrollCountNum) + var finalY = tableView.contentOffset.y + dx + ratio*scrollCountViewHeight + ratio*self.rectHeight if (finalY < 0) { finalY = 0 - } else if(finalY > tableView.contentSize.height - scrollCountViewHeight) { - finalY = tableView.contentSize.height - scrollCountViewHeight + } else if(finalY > (tableView.contentOffset.y + self.rectHeight - scrollCountViewHeight)) { + var difY = finalY - (tableView.contentOffset.y + self.rectHeight - scrollCountViewHeight) + finalY -= difY } - topConstraint.constant = finalY } } - }