Skip to content

Commit

Permalink
fix: cleanup shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
vcellu committed Nov 7, 2022
1 parent c740932 commit 0aa9250
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 55 deletions.
70 changes: 30 additions & 40 deletions ios/ColumnResizerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ColumnResizerView: UIView {
weak var button: ResizerButtonView?
weak var horizontalScrollView: UIScrollView?
weak var containerView: ContainerView?

init( _ columnWidths: ColumnWidths, index: Int, bindTo bindedTableView: TableView) {
self.columnWidths = columnWidths
self.tableView = bindedTableView
Expand All @@ -30,7 +30,7 @@ class ColumnResizerView: UIView {
self.isUserInteractionEnabled = true
createButton()
}

fileprivate func createButton() {
let button = ResizerButtonView()
button.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -44,49 +44,39 @@ class ColumnResizerView: UIView {
]
NSLayoutConstraint.activate(constraints)
self.addConstraints(constraints)

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.handleGesture(_:)))
panGesture.minimumNumberOfTouches = 1
panGesture.maximumNumberOfTouches = 1
button.isUserInteractionEnabled = true
button.addGestureRecognizer(panGesture)
self.button = button
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
if let touch = touches.first {
if touch.view == self.button {
pressed = true
setNeedsDisplay()
}
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if let touch = touches.first {
if touch.view == self.button {
pressed = false
setNeedsDisplay()
}
super.touchesBegan(touches, with: event)
if let touch = touches.first {
if touch.view == self.button {
pressed = true
setNeedsDisplay()
}
}

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
if view == button {
return view
}
if view == self {
return nil
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if let touch = touches.first {
if touch.view == self.button {
pressed = false
setNeedsDisplay()
}
return view
}

}

@objc func handleGesture(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
pressed = true
self.setNeedsDisplay()
case .changed:
let point = sender.translation(in: self)
Expand All @@ -109,14 +99,14 @@ class ColumnResizerView: UIView {
break
}
}

func didPan(_ translation: CGPoint) {
guard let tableView = self.tableView else { return }
guard let data = tableView.dataCollectionView else { return }
if data.updateSize(translation, withColumn: index) {
columnWidths.resize(index: index, by: translation)
self.centerConstraint.constant = self.centerConstraint.constant + translation.x

tableView.grow(by: translation.x)
if let adjacentTable = self.adjacentTable {
adjacentTable.dymaniceLeadingAnchor.constant = columnWidths.columnWidths[0]
Expand All @@ -129,7 +119,7 @@ class ColumnResizerView: UIView {
tableView.layoutIfNeeded()
}
}

func didEndPand() {
columnWidths.saveToStorage()
if let scrollView = self.horizontalScrollView {
Expand All @@ -139,23 +129,23 @@ class ColumnResizerView: UIView {
}
}
}

func setPosition(_ x: Double) {
self.centerConstraint.constant = x
}

override func draw(_ rect: CGRect) {
let x = rect.origin.x + rect.width / 2
linePath.removeAllPoints()
linePath.move(to: CGPoint(x: x, y: 0))
linePath.addLine(to: CGPoint(x: x, y: rect.height))
linePath.lineWidth = 1

let color = pressed ? .black : borderColor
color.setStroke()
linePath.stroke()
}

func setHeight(_ newVal: Double) {
guard let button = button else { return }
button.heightConstraint.constant = newVal
Expand All @@ -166,16 +156,16 @@ class ColumnResizerView: UIView {
if let headerView = self.headerView {
headerView.updateSize(translation, withColumn: index)
}

}
func updateTotals(_ translation: CGPoint) {
if let totals = self.totalsView {
totals.updateSize(translation, withColumn: index)
}
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}
21 changes: 7 additions & 14 deletions ios/HorizontalScrolViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation
class HorizontalScrollViewDelegate: NSObject, UIScrollViewDelegate {

weak var tableView: TableView?
weak var totalsView: UIView?
weak var headersView: UIView?
Expand All @@ -25,7 +24,7 @@ class HorizontalScrollViewDelegate: NSObject, UIScrollViewDelegate {
guard let tableView = self.tableView else { return }
guard let columnWidths = self.columnWidths else { return }
guard let grabber = self.grabber else { return }
let shadowOffsetX = clampScrollPos(scrollView.contentOffset.x)
let shadowOffsetX = clampScrollPos(Float(scrollView.contentOffset.x))
let rawX = scrollView.contentOffset.x

if rawX <= 0 {
Expand All @@ -39,22 +38,16 @@ class HorizontalScrollViewDelegate: NSObject, UIScrollViewDelegate {
}

let offset = shadowOffsetX/100.0
updateShadow(tableView, offset: offset)

if let totalsView = totalsView {
updateShadow(totalsView, offset: offset)
}

if let headersView = headersView {
updateShadow(headersView, offset: offset)
}
updateShadow(offset: offset)

}

func clampScrollPos(_ x: CGFloat) -> CGFloat {
func clampScrollPos(_ x: Float) -> Float {
return min(100.0, x)
}

func updateShadow(_ view: UIView, offset: CGFloat) {
view.addLeftShadow(radius: 2, opacity: offset, offset: offset)
func updateShadow(offset: Float) {
tableView?.setShadow(offset: min(offset, 0.4))
tableView?.setNeedsDisplay()
}
}
10 changes: 9 additions & 1 deletion ios/TableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class TableView: UIView {
weak var lastGrabber: LastColumnResizer?
weak var firstGrabber: ColumnResizerView?
weak var adjacentTable: TableView?
var needsShadow = false
var shaddowOffset:Float = 0.0
var isFirst = false
var dynamicWidth = NSLayoutConstraint()
var dymaniceLeadingAnchor = NSLayoutConstraint()
Expand Down Expand Up @@ -97,5 +99,11 @@ class TableView: UIView {
resizer()?.setHeight(height)
}
}


func setShadow(offset: Float) {
self.layer.shadowOffset = CGSize(width: 1, height: 1)
self.layer.shadowRadius = 2
self.layer.shadowColor = UIColor.gray.cgColor
self.layer.shadowOpacity = offset
}
}

0 comments on commit 0aa9250

Please sign in to comment.