@@ -22,11 +22,26 @@ class ChatViewController: UIViewController {
2222 fileprivate var messages : [ Message ] = [ ]
2323
2424 @IBOutlet weak var collectionView : UICollectionView !
25+
26+ //MARK: - View & VC life cycle
2527 override func viewDidLoad( ) {
2628 super. viewDidLoad ( )
2729
2830 messages = NSArray ( contentsOfFile: Bundle . main. path ( forResource: " YALChatDemoList " , ofType: " plist " ) !) as! [ Message ]
2931 }
32+
33+ override func viewWillAppear( _ animated: Bool ) {
34+ super. viewWillAppear ( animated)
35+
36+ prepareVisibleCellsForAnimation ( )
37+ }
38+
39+ override func viewDidAppear( _ animated: Bool ) {
40+ super. viewDidAppear ( animated)
41+
42+ animateVisibleCells ( )
43+ }
44+
3045}
3146
3247extension ChatViewController : UICollectionViewDataSource {
@@ -65,3 +80,27 @@ extension ChatViewController {
6580 segue. destination. hidesBottomBarWhenPushed = true
6681 }
6782}
83+
84+ //MARK: - Cell's animation
85+ private extension ChatViewController {
86+
87+ func prepareVisibleCellsForAnimation( ) {
88+ for itemIndex in 0 ..< collectionView. visibleCells. count {
89+ let cell = collectionView. cellForItem ( at: IndexPath ( item: itemIndex, section: 0 ) ) as! ChatCollectionViewCell
90+ cell. frame = CGRect ( x: - cell. bounds. width, y: cell. frame. origin. y, width: cell. bounds. width, height: cell. bounds. height)
91+ cell. alpha = 0
92+ }
93+ }
94+
95+ func animateVisibleCells( ) {
96+ for itemIndex in 0 ..< collectionView. visibleCells. count {
97+ let cell = collectionView. cellForItem ( at: IndexPath ( item: itemIndex, section: 0 ) ) as! ChatCollectionViewCell
98+ cell. alpha = 1
99+ UIView . animate ( withDuration: 0.25 , delay: Double ( itemIndex) * 0.1 , options: . curveEaseOut, animations: {
100+ cell. frame = CGRect ( x: 0 , y: cell. frame. origin. y, width: cell. bounds. width, height: cell. bounds. height)
101+ } )
102+
103+ }
104+ }
105+
106+ }
0 commit comments