I have a collection view in my UIViewController class and use an array of Booleans to see which indices are expanded (refer to this answer).
var isExpanded = [Bool]()
So when I click a button, I am able to reload the cell with:
@objc func topButtonTouched(indexPath: IndexPath) {
print(indexPath)
isExpanded[indexPath.row] = !isExpanded[indexPath.row]
UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1.0, options: UIView.AnimationOptions.transitionCurlUp, animations: {
self.listCollectionView.reloadItems(at: [indexPath])
}, completion: { success in
print("success")
})
}
So far so good. However, the issue I am having appears when the cells are reordered. I am not reordering with beginInteractiveMovementForItem(), updateInteractiveMovementTargetPosition(), etc.
I started using this function (from this repository) below to create a scaling animation (of the snapshot of the cell) thinking that it would reorder the cells as well:
@objc func longPressRecognized(_ recognizer: UILongPressGestureRecognizer) {
let location = recognizer.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: location)
switch recognizer.state {
case UIGestureRecognizer.State.began:
guard let indexPath = indexPath else { return }
let cell = cellForRow(at: indexPath)
snapshotView = cell.snapshotView(afterScreenUpdates: true)
collectionView.addSubview(snapshotView!)
cell.contentView.alpha = 0.0
UIView.animate(withDuration: 0.2, animations: {
self.snapshotView?.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
self.snapshotView?.alpha = 0.9
})
snapshotPanPoint = location
snapshotIndexPath = indexPath
case UIGestureRecognizer.State.changed:
guard let snapshotPanPoint = snapshotPanPoint else { return }
let translation = CGPoint(x: location.x - snapshotPanPoint.x, y: location.y - snapshotPanPoint.y)
snapshotView?.center.y += translation.y
self.snapshotPanPoint = location
guard let indexPath = indexPath else { return }
collectionView.moveItem(at: snapshotIndexPath!, to: indexPath)
snapshotIndexPath = indexPath
default:
guard let snapshotIndexPath = snapshotIndexPath else { return }
let cell = cellForRow(at: snapshotIndexPath)
UIView.animate(
withDuration: 0.2,
animations: {
self.snapshotView?.center = cell.center
self.snapshotView?.transform = CGAffineTransform.identity
self.snapshotView?.alpha = 1.0
},
completion: { finished in
cell.contentView.alpha = 1.0
self.snapshotView?.removeFromSuperview()
self.snapshotView = nil
})
self.snapshotIndexPath = nil
self.snapshotPanPoint = nil
}
}
I've tried changing the isExpanded array in the .changed section of the switch but had no luck. I also tried this answer.
Gif of my problem:
Edit: Before when I had a single array for bools, it was like this:
@objc func topButtonTouched(indexPath: IndexPath) {
isExpanded[indexPath.item] = !isExpanded[indexPath.item]
and now with the main array of objects, I am trying to do this:
@objc func topButtonTouched(indexPath: IndexPath) {
var names = mainData.map({ $0.isExpanded })
names[indexPath.row] = !names[indexPath.row]
and then update the indexpaths.