Cách tốt nhất để đạt được hoạt ảnh trên ô UICollectionView
là ghi đè bố cục UICollectionViewLayout
. Phương thức của nó sẽ trả về các thuộc tính layout của ô mà bạn muốn xuất hiện/insert/delete.
Ví dụ: Tôi đã tạo một lớp KDCollectionViewFlowLayout
kế thừa UICollectionViewFlowLayout
và ghi đè thuộc tính xóa.
class KDCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attribute = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath)
attribute?.transform = CGAffineTransformTranslate(attributes.transform, 0, ITEM_SIZE)
attribute?.alpha = 0.0
return attribute
}
}
Bây giờ bạn cần phải gán đối tượng của flowLayout này để xem bộ sưu tập trong cả hai viewDidLoad hoặc bạn có thể gán nó thông qua kịch bản này.
let flowLayout = KDCollectionViewFlowLayout()
self.collectionView?.setCollectionViewLayout(flowLayout, animated: true)
Bây giờ, bạn đều đặt ra cho sự biến đổi của tế bào mà bạn quy định tại để finalLayoutAttributesForDisappearingItemAtIndexPath
phương pháp bất cứ khi nào bạn thực hiện bất kỳ hoạt động xóa trên collectionView
.
Cập nhật
Bạn cần xóa các mục khỏi tầm nhìn bộ sưu tập sử dụng hoạt động hàng loạt.
collectionView.performBatchUpdates({() -> Void in
//Array of the data which you need to deleted from collection view
let indexPaths = [NSIndexPath]()
//Delete those entery from the data base.
//TODO: Delete the information from database
//Now Delete those row from collection View
collectionView.deleteItemsAtIndexPaths(indexPaths)
}, completion:nil)
Ứng dụng của bạn trông tuyệt vời! –
James, bạn đã từng làm việc này như bạn dự định chưa? Tìm cách triển khai chính xác cùng một loại chức năng trong Swift. Rất thích xem ứng dụng của bạn, nếu làm việc quá! –