Closed Megaman63 closed 7 years ago
simple profect:
import UIKit import IGListKit
class Model : NSObject, IGListDiffable {
var id = 0
var color : UIColor!
var height : CGFloat = 0
var expanded = false
func diffIdentifier() -> NSObjectProtocol {
return id as NSObjectProtocol
}
func isEqual(toDiffableObject object: IGListDiffable?) -> Bool {
return id == (object as! Model).id
}
}
class ViewController: UIViewController, IGListAdapterDataSource { var models : [Model] = [] var adapter : IGListAdapter! @IBOutlet var collectionView: IGListCollectionView! override func viewDidLoad() { super.viewDidLoad()
for i in 0...100 {
let model = Model()
let r = arc4random()%255
let g = arc4random()%255
let b = arc4random()%255
model.id = i
model.color = UIColor(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: 1)
model.height = CGFloat(arc4random() % 200 + 50)
models.append(model)
}
adapter = IGListAdapter(updater: IGListAdapterUpdater(), viewController: self, workingRangeSize: 0)
adapter.collectionView = collectionView
adapter.dataSource = self
collectionView.backgroundColor = UIColor(red: 0.831372549, green: 0.945098039, blue: 0.964705882, alpha: 1)
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
//flowLayout.estimatedItemSize = CGSize(width: collectionView.frame.width, height: 150)
}
//MARK: IGListAdapterDataSource
func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
return models as [IGListDiffable]
}
func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController {
let f = FeedSectionController()
f.controller = self
return f
}
func emptyView(for listAdapter: IGListAdapter) -> UIView? { return nil }
}
class FeedSectionController: IGListSectionController, IGListSectionType {
public func didSelectItem(at index: Int) {}
var controller : ViewController!
var object : Model!
var expanded = false
override init() {
super.init()
inset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
minimumLineSpacing = 4
minimumInteritemSpacing = 4
}
func numberOfItems() -> Int {
return 1
}
func sizeForItem(at index: Int) -> CGSize {
return CGSize(width: collectionContext!.containerSize.width, height: object!.expanded ? object!.height * 2 : object!.height)
}
func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = collectionContext!.dequeueReusableCellFromStoryboard(withIdentifier: "Cell", for: self, at: index) as! Cell
cell.imageView.backgroundColor = object!.color
cell.height.constant = object!.height
cell.section = self
cell.model = object
cell.layoutIfNeeded()
return cell
}
func didUpdate(to object: Any) {
self.object = (object as! Model)
}
func update(_ expanded: Bool) {
object!.expanded = expanded
controller.adapter.performUpdates(animated: true, completion: nil)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.6,
options: [],
animations: {
self.controller.collectionView.collectionViewLayout.invalidateLayout()
})
}
}
class Cell : UICollectionViewCell { @IBOutlet var imageView: UIImageView! @IBOutlet var height: NSLayoutConstraint! var expanded = false var model : Model! weak var section : FeedSectionController!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
height.constant = expanded ? model.height * 2 : model.height
expanded = !expanded
print ("height \(height.constant)")
section.update(expanded)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.6,
options: [],
animations: {
self.contentView.layoutIfNeeded()
})
}
}
@Megaman63 This is a new API that will be added in 3.0.0 release. If you want to use it now, you can use master branch version instead of 2.0.0 release.
pod 'IGListKit', :git => 'https://github.com/Instagram/IGListKit.git', :branch => 'master'
New issue checklist
README
and documentationGeneral information
IGListKit
version: 2.0.0