Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly restructured calucation for cell counter #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MRTableViewCellCountScrollIndicator.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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
Expand All @@ -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
}
}

}