Created
April 18, 2023 07:18
-
-
Save patryk-sredzinski/832008c265bff1d99e29dd22da844ecd to your computer and use it in GitHub Desktop.
automaticallyAdjustsContentOffset - not working properly when user is scrolling, or there is a pending scrolling animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TestController.swift | |
// TestScroll | |
// | |
// Created by Patryk Średziński on 18/04/2023. | |
// | |
import AsyncDisplayKit | |
final class TestNode: ASDisplayNode { | |
lazy var tableNode: ASTableNode = { | |
let tableNode = ASTableNode(style: .plain) | |
tableNode.automaticallyAdjustsContentOffset = true | |
tableNode.style.flexGrow = 1 | |
return tableNode | |
}() | |
lazy var buttonNode: ASButtonNode = { | |
let buttonNode = ASButtonNode() | |
buttonNode.setTitle("Add Elements On Top", with: .boldSystemFont(ofSize: 20), with: .white, for: .normal) | |
buttonNode.setTitle("Add Elements On Top", with: .boldSystemFont(ofSize: 22), with: .lightGray, for: .highlighted) | |
buttonNode.style.height = ASDimensionMakeWithPoints(60) | |
return buttonNode | |
}() | |
override init() { | |
super.init() | |
automaticallyManagesSubnodes = true | |
} | |
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { | |
let stackSpec = ASStackLayoutSpec.vertical() | |
stackSpec.children = [ | |
tableNode, | |
buttonNode | |
] | |
return stackSpec | |
} | |
} | |
final class CellNode: ASCellNode { | |
let textNode = ASTextNode() | |
override init() { | |
super.init() | |
automaticallyManagesSubnodes = true | |
backgroundColor = .brown | |
style.height = ASDimensionMakeWithPoints(50) | |
} | |
override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec { | |
return ASWrapperLayoutSpec(layoutElement: textNode) | |
} | |
} | |
final class TestController: ASDKViewController<TestNode>, ASTableDelegate, ASTableDataSource { | |
var elements = [Int]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
node.tableNode.dataSource = self | |
node.tableNode.delegate = self | |
node.buttonNode.addTarget(self, action: #selector(addElemenentsOnTop), forControlEvents: .touchUpInside) | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
addElemenentsOnTop() | |
} | |
@objc func addElemenentsOnTop() { | |
let count = 50 | |
let currentFirst = elements.first ?? 0 | |
var appendingElements = [Int]() | |
var appendingIndexes = [IndexPath]() | |
for i in 0..<count { | |
let startIndex = currentFirst - count | |
appendingElements.append(startIndex + i) | |
appendingIndexes.append(IndexPath(row: i, section: 0)) | |
} | |
elements.insert(contentsOf: appendingElements, at: 0) | |
node.tableNode.performBatch(animated: false, updates: { | |
node.tableNode.insertRows(at: appendingIndexes, with: .none) | |
}, completion: { _ in | |
print("DONE") | |
}) | |
} | |
func numberOfSections(in tableNode: ASTableNode) -> Int { | |
return 1 | |
} | |
func tableNode(_ tableNode: ASTableNode, numberOfRowsInSection section: Int) -> Int { | |
return elements.count | |
} | |
func tableNode(_ tableNode: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock { | |
let data = elements[indexPath.row] | |
return { | |
let cell = CellNode() | |
cell.textNode.attributedText = NSAttributedString(string: "Cell #\(data)", attributes: [ | |
.font: UIFont.boldSystemFont(ofSize: 17) | |
]) | |
return cell | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment