From 753c6fdbfc0eb1ef66e3550cab739c4534cc345a Mon Sep 17 00:00:00 2001 From: Jerome Chan Date: Wed, 6 Apr 2022 16:01:47 +0800 Subject: [PATCH] Added code to ScrollableSegmentedControl to highlight a tab without animating --- .../ScrollableSegmentedControl.swift | 31 +++++++++++++++---- .../TokensViewController.swift | 2 +- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/AlphaWallet/ScrollableSegmentedControl/ScrollableSegmentedControl.swift b/AlphaWallet/ScrollableSegmentedControl/ScrollableSegmentedControl.swift index 29bf798d1..9485e8a26 100644 --- a/AlphaWallet/ScrollableSegmentedControl/ScrollableSegmentedControl.swift +++ b/AlphaWallet/ScrollableSegmentedControl/ScrollableSegmentedControl.swift @@ -124,16 +124,16 @@ class ScrollableSegmentedControl: UIControl, ReusableTableHeaderViewType { // MARK: - Set selection // MARK: Public - func setSelection(cellIndex: Int) { + func setSelection(cellIndex: Int, animated: Bool = true) { guard cellIndex < cells.count, cellIndex >= 0 else { return } _selectedSegment = .selected(UInt(cellIndex)) - highlightCell(cellIndex: cellIndex) + animated ? highlightCellWithAnimation(cellIndex: cellIndex) : highlightCellWithoutAnimation(cellIndex: cellIndex) } - func setSelection(cell: ScrollableSegmentedControlCell) { + func setSelection(cell: ScrollableSegmentedControlCell, animated: Bool = true) { guard let cellIndex = cells.firstIndex(of: cell) else { return } _selectedSegment = .selected(UInt(cellIndex)) - highlightCell(cellIndex: cellIndex) + animated ? highlightCellWithAnimation(cellIndex: cellIndex) : highlightCellWithoutAnimation(cellIndex: cellIndex) } func unselect() { @@ -143,7 +143,7 @@ class ScrollableSegmentedControl: UIControl, ReusableTableHeaderViewType { // MARK: Private - private func highlightCell(cellIndex: Int) { + private func highlightCellWithAnimation(cellIndex: Int) { guard cellIndex >= 0, cellIndex < cells.count else { return } let animation = UIViewPropertyAnimator(duration: configuration.animationDuration, curve: configuration.animationCurve) { self.unhighlightAllCells() @@ -151,7 +151,7 @@ class ScrollableSegmentedControl: UIControl, ReusableTableHeaderViewType { if highlightedCell.frame.size == .zero { // Cell is not yet rendered so we loop until it is. This only happens the first time the control is rendered in a tableview as a header. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - self.highlightCell(cellIndex: cellIndex) + self.highlightCellWithAnimation(cellIndex: cellIndex) } return } @@ -165,6 +165,25 @@ class ScrollableSegmentedControl: UIControl, ReusableTableHeaderViewType { animation.startAnimation() } + private func highlightCellWithoutAnimation(cellIndex: Int) { + guard cellIndex >= 0, cellIndex < cells.count else { return } + let highlightedCell = self.cells[cellIndex] + if highlightedCell.frame.size == .zero { + // Cell is not yet rendered so we loop until it is. This only happens the first time the control is rendered in a tableview as a header. + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { + self.highlightCellWithoutAnimation(cellIndex: cellIndex) + } + return + } + self.unhighlightAllCells() + highlightedCell.highlighted = true + let frame = self.line.convert(highlightedCell.bounds, from: highlightedCell) + self.line.lineStartOffset = frame.minX + self.line.lineEndOffset = frame.maxX + self.line.layoutIfNeeded() + self.scrollView.scrollRectToVisible(highlightedCell.frame, animated: false) + } + private func unhighlightLine() { let animation = UIViewPropertyAnimator(duration: configuration.animationDuration, curve: configuration.animationCurve) { self.line.lineStartOffset = 0 diff --git a/AlphaWallet/Tokens/ViewControllers/TokensViewController.swift b/AlphaWallet/Tokens/ViewControllers/TokensViewController.swift index efffcf50e..89a8877d0 100644 --- a/AlphaWallet/Tokens/ViewControllers/TokensViewController.swift +++ b/AlphaWallet/Tokens/ViewControllers/TokensViewController.swift @@ -35,7 +35,7 @@ class TokensViewController: UIViewController { ScrollableSegmentedControlCell(frame: .zero, title: title, configuration: cellConfiguration) } let control = ScrollableSegmentedControl(cells: cells, configuration: controlConfiguration) - control.setSelection(cellIndex: 0) + control.setSelection(cellIndex: 0, animated: false) return control }() private let emptyTableView: EmptyTableView = {