UICollectionView is unresponsive during cell resize animations despite .allowUserInteraction option

I’m experiencing an issue with UICollectionView where the cells become unresponsive to touch events during animations, even though I’m using the .allowUserInteraction option. I’ve created a minimal reproducible example to demonstrate the problem.

Problem Description

In my UICollectionView, I want to resize cells when they are selected. The resizing works as expected, but the cells that are participating in the animations do not respond to scroll or touch events during the animation. I’ve tried using the .allowUserInteraction option, but it doesn’t seem to help.

Minimal Reproducible Example

Here’s the code for the UICollectionView setup:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import UIKit
class ExpandableCell: UICollectionViewCell {
static let identifier = "ExpandableCell"
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .lightGray
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hit = super.hitTest(point, with: event)
print(hit, hit?.isUserInteractionEnabled)
return hit
}
}
class ExpandableViewController: UIViewController {
private var selectedIndexPath: IndexPath?
private let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
return UICollectionView(frame: .zero, collectionViewLayout: layout)
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.frame = view.bounds
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(ExpandableCell.self, forCellWithReuseIdentifier: ExpandableCell.identifier)
}
}
extension ExpandableViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ExpandableCell.identifier, for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndexPath = indexPath
UIView.animate(withDuration: 3, delay: 0, options: [.allowUserInteraction]) {
collectionView.collectionViewLayout.invalidateLayout()
collectionView.layoutIfNeeded()
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let isSelected = selectedIndexPath == indexPath
return .init(width: collectionView.bounds.width, height: isSelected ? 200 : 100)
}
}
@available(iOS 17.0, *)
#Preview {
ExpandableViewController()
}
</code>
<code>import UIKit class ExpandableCell: UICollectionViewCell { static let identifier = "ExpandableCell" override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .lightGray } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let hit = super.hitTest(point, with: event) print(hit, hit?.isUserInteractionEnabled) return hit } } class ExpandableViewController: UIViewController { private var selectedIndexPath: IndexPath? private let collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() return UICollectionView(frame: .zero, collectionViewLayout: layout) }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(collectionView) collectionView.frame = view.bounds collectionView.delegate = self collectionView.dataSource = self collectionView.register(ExpandableCell.self, forCellWithReuseIdentifier: ExpandableCell.identifier) } } extension ExpandableViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ExpandableCell.identifier, for: indexPath) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { selectedIndexPath = indexPath UIView.animate(withDuration: 3, delay: 0, options: [.allowUserInteraction]) { collectionView.collectionViewLayout.invalidateLayout() collectionView.layoutIfNeeded() } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let isSelected = selectedIndexPath == indexPath return .init(width: collectionView.bounds.width, height: isSelected ? 200 : 100) } } @available(iOS 17.0, *) #Preview { ExpandableViewController() } </code>
import UIKit

class ExpandableCell: UICollectionViewCell {
    static let identifier = "ExpandableCell"

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .lightGray
    }

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

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hit = super.hitTest(point, with: event)
        print(hit, hit?.isUserInteractionEnabled)
        return hit
    }
}

class ExpandableViewController: UIViewController {
    private var selectedIndexPath: IndexPath?
    private let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        return UICollectionView(frame: .zero, collectionViewLayout: layout)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
        collectionView.frame = view.bounds
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ExpandableCell.self, forCellWithReuseIdentifier: ExpandableCell.identifier)
    }
}

extension ExpandableViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ExpandableCell.identifier, for: indexPath)
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedIndexPath = indexPath
        UIView.animate(withDuration: 3, delay: 0, options: [.allowUserInteraction]) {
            collectionView.collectionViewLayout.invalidateLayout()
            collectionView.layoutIfNeeded()
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let isSelected = selectedIndexPath == indexPath
        return .init(width: collectionView.bounds.width, height: isSelected ? 200 : 100)
    }
}

@available(iOS 17.0, *)
#Preview {
    ExpandableViewController()
}

Observations

  • I verified that all cells’ isUserInteractionEnabled property is true at all times.
  • The scroll or touch inputs onto the cells that are not participating in the animations are handled correctly.

Question

Why are the cells not responding to touch events during the animation, despite using the .allowUserInteraction option? How can I ensure that the cells remain responsive to touch events during the animation?

Any insights or suggestions would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật