Skip to content

Commit

Permalink
topY related bug fix (issue #3)
Browse files Browse the repository at this point in the history
jumping while dragging from top to down is fixed
some comments are added for initial parameters
  • Loading branch information
OfTheWolf committed Nov 8, 2018
1 parent ff058fe commit 4ac62c3
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions UBottomSheet/BottomSheetViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ class BottomSheetViewController: UIViewController{
var parentView: UIView!

var initalFrame: CGRect!
let topY: CGFloat = 80
var middleY: CGFloat = 400
var bottomY: CGFloat = 600
let bottomOffset: CGFloat = 64
var lastLevel: SheetLevel = .middle
var topY: CGFloat = 80 //change this in viewWillAppear for top position
var middleY: CGFloat = 400 //change this in viewWillAppear to decide if animate to top or bottom
var bottomY: CGFloat = 600 //no need to change this
let bottomOffset: CGFloat = 64 //sheet height on bottom position
var lastLevel: SheetLevel = .middle //choose inital position of the sheet

var disableTableScroll = false

//hack panOffset To prevent jump when goes from top to down
var panOffset: CGFloat = 0
var applyPanOffset = false

//tableview variables
var listItems: [Any] = []
var headerItems: [Any] = []

Expand All @@ -53,6 +59,7 @@ class BottomSheetViewController: UIViewController{
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.initalFrame = UIScreen.main.bounds
self.topY = round(initalFrame.height * 0.05)
self.middleY = initalFrame.height * 0.6
self.bottomY = initalFrame.height - bottomOffset
self.lastY = self.middleY
Expand Down Expand Up @@ -80,24 +87,32 @@ class BottomSheetViewController: UIViewController{
}

@objc func handlePan(_ recognizer: UIPanGestureRecognizer){
if self.tableView.contentOffset.y > 0{return}

let dy = recognizer.translation(in: self.parentView).y
switch recognizer.state {
case .began:
applyPanOffset = (self.tableView.contentOffset.y > 0)
case .changed:
if self.tableView.contentOffset.y > 0{
panOffset = dy
return
}

if self.tableView.contentOffset.y <= 0{
let maxY = max(topY, lastY + dy)
if !applyPanOffset{panOffset = 0}
let maxY = max(topY, lastY + dy - panOffset)
let y = min(bottomY, maxY)
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: y)
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: y)
bottomSheetDelegate?.updateBottomSheet(frame: self.initalFrame.offsetBy(dx: 0, dy: y))
}

if self.parentView.frame.minY > topY{
self.tableView.contentOffset.y = 0
}
case .failed, .ended, .cancelled:
panOffset = 0
self.panView.isUserInteractionEnabled = false

self.disableTableScroll = self.lastLevel != .top

self.lastY = self.parentView.frame.minY
Expand All @@ -107,22 +122,21 @@ class BottomSheetViewController: UIViewController{

switch self.lastLevel{
case .top:
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: self.topY)
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: self.topY)
self.bottomSheetDelegate?.updateBottomSheet(frame: self.initalFrame.offsetBy(dx: 0, dy: self.topY))
self.tableView.contentInset.bottom = self.topY + 50
self.tableView.contentInset.bottom = 50
case .middle:
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: self.middleY)
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: self.middleY)
self.bottomSheetDelegate?.updateBottomSheet(frame: self.initalFrame.offsetBy(dx: 0, dy: self.middleY))
case .bottom:
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: self.bottomY)
// self.panView.frame = self.initalFrame.offsetBy(dx: 0, dy: self.bottomY)
self.bottomSheetDelegate?.updateBottomSheet(frame: self.initalFrame.offsetBy(dx: 0, dy: self.bottomY))
}

}) { (_) in
self.panView.isUserInteractionEnabled = true
self.lastY = self.parentView.frame.minY
}

default:
break
}
Expand Down

0 comments on commit 4ac62c3

Please sign in to comment.