@@ -10,11 +10,12 @@ import UIKit
1010
1111
1212open class IMUIMessageCollectionView : UIView {
13-
13+
1414 @IBOutlet var view : UIView !
1515 @IBOutlet open weak var messageCollectionView : UICollectionView !
1616 var isPull = false
1717 var viewCache = IMUIReuseViewCache ( )
18+ var headView = IMUIBaseMessageHeadCell ( )
1819
1920 // React Native Property ---->
2021 var action : Array < Any > {
@@ -44,8 +45,9 @@ open class IMUIMessageCollectionView: UIView {
4445 view. frame = self . bounds
4546 self . chatDataManager = IMUIChatDataManager ( )
4647 self . setupMessageCollectionView ( )
48+
4749 }
48-
50+
4951 required public init ? ( coder aDecoder: NSCoder ) {
5052 super. init ( coder: aDecoder)
5153
@@ -80,8 +82,11 @@ open class IMUIMessageCollectionView: UIView {
8082 self . messageCollectionView. register ( IMUIRedPacketMessageCell . self, forCellWithReuseIdentifier: IMUIRedPacketMessageCell . self. description ( ) )
8183 self . messageCollectionView. register ( IMUITransferMessageCell . self, forCellWithReuseIdentifier: IMUITransferMessageCell . self. description ( ) )
8284 self . messageCollectionView. register ( IMUIRedPacketOpenMessageCell . self, forCellWithReuseIdentifier: IMUIRedPacketOpenMessageCell . self. description ( ) )
85+ self . messageCollectionView. register ( IMUIBaseMessageHeadCell . self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: " headView " )
8386
8487 self . messageCollectionView. isScrollEnabled = true
88+
89+ NotificationCenter . default. addObserver ( self , selector: #selector( clickStopPlayActivity ( notification: ) ) , name: NSNotification . Name ( rawValue: " StopPlayActivity " ) , object: nil )
8590 }
8691
8792 open subscript( index: Int ) -> IMUIMessageModelProtocol {
@@ -138,13 +143,15 @@ open class IMUIMessageCollectionView: UIView {
138143 }
139144
140145 open func insertMessages( with messages: [ IMUIMessageModel ] ) {
146+
141147 self . chatDataManager. insertMessages ( with: messages)
142148 self . messageCollectionView. reloadData ( )
143- var scrollIndex = messages. count
144- if scrollIndex> 1 {
145- scrollIndex = scrollIndex - 1
146- }
149+ let scrollIndex = messages. count
150+ // if scrollIndex>1 {
151+ // scrollIndex = scrollIndex
152+ // }
147153 self . scrollTo ( index: scrollIndex)
154+
148155 }
149156
150157 open func updateMessage( with message: IMUIMessageModel ) {
@@ -154,6 +161,16 @@ open class IMUIMessageCollectionView: UIView {
154161 self . messageCollectionView. reloadItems ( at: [ indexPath] )
155162 }
156163 }
164+ deinit {
165+ NotificationCenter . default. removeObserver ( self )
166+ }
167+
168+ //通知方法
169+ func clickStopPlayActivity( notification: Notification ) {
170+ DispatchQueue . main. async ( execute: {
171+ self . headView. stopActView ( )
172+ } )
173+ }
157174}
158175
159176// MARK: - UICollectionViewDelegate, UICollectionViewDataSource
@@ -247,20 +264,38 @@ extension IMUIMessageCollectionView: UICollectionViewDelegate, UICollectionViewD
247264 self . delegate? . messageCollectionView ? ( collectionView, didEndDisplaying: didEndDisplaying, forItemAt: forItemAt, model: messageModel)
248265 }
249266 }
250- }
267+
268+ public func collectionView( _ collectionView: UICollectionView , layout collectionViewLayout: UICollectionViewLayout , referenceSizeForHeaderInSection section: Int ) -> CGSize {
269+ return CGSize ( width: UIScreen . main. bounds. width, height: 30 )
270+ }
251271
272+ public func collectionView( _ collectionView: UICollectionView , viewForSupplementaryElementOfKind kind: String , at indexPath: IndexPath ) -> UICollectionReusableView {
273+ let headCell = collectionView. dequeueReusableSupplementaryView ( ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: " headView " , for: indexPath) as! IMUIBaseMessageHeadCell
274+ self . headView = headCell
275+ return headCell
276+ }
277+
278+ }
252279
253280
254281extension IMUIMessageCollectionView : UIScrollViewDelegate {
255282 public func scrollViewWillBeginDragging( _ scrollView: UIScrollView ) {
256283 self . delegate? . messageCollectionView ? ( self . messageCollectionView)
257284 }
258285 public func scrollViewDidScroll( _ scrollView: UIScrollView ) {
286+ DispatchQueue . main. async ( execute: {
287+ self . headView. playActView ( )
288+ } )
259289 if scrollView. contentOffset. y < - 30 {
260290 isPull = true
261291 } else if scrollView. contentOffset. y == 0 && isPull{
292+
262293 isPull = false
263294 self . delegate? . messageCollectionView ? ( reloadMoreData: " " )
295+ } else {
296+ DispatchQueue . main. async ( execute: {
297+ self . headView. stopActView ( )
298+ } )
264299 }
265300 }
266301
0 commit comments