SoFunction
Updated on 2025-03-03

IOS implements simple barrage function

Preface

It simply implements the barrage function, and talks with me about efficiency, but it also uses queues to control the number of bullets at the same time.

text

Code implementation:

let DANMAKU_SPEED: CGFloat = 150 // Barrage movement speed per second let DANMAKU_SPACE_TIME: NSTimeInterval = 1 // Time interval between barrage let DANMAKU_MAX_ROW = 3 // Maximum number of barrage lines at the same time let danmakuFont = (18) // Barrage font size var rowArray = Array<Array<Danmaku>>(count: 3, repeatedValue: Array<Danmaku>()) 
 var danmakuQueue = NSOperationQueue() // Queue
 class Danmaku : NSObject{
  var msg: Msg
  var view: UILabel?
  var size = CGSize(width: 0, height: 0)
  var row = 0
  var startTime: NSDate?
  var duration: NSTimeInterval = 0
  var delay: NSTimeInterval = 0
  
  init(_ msg: Msg, _ row: Int, _ delay: NSTimeInterval = 0) {
    = msg
    = row
    = delay
  }
 }
 
 func queueDanmaku(msg: Msg) {
  (NSBlockOperation(block: { [weak self] in

   if let weakself = self {
    repeat {
     //How many rows are placed     for var row = 0; row < weakself.DANMAKU_MAX_ROW; ++row {
      let rowDanmaku = [row]
      if  == 0 {
       let danmaku = Danmaku(msg, , row)
       [row].append(danmaku)
       self?.performSelectorOnMainThread("sendDanmaku:", withObject: danmaku, waitUntilDone: true)
       return
      } else {
       if let lastDanmaku =  {
        if let startTime =  {
         let now = NSDate()
         let seconds = (startTime)
         let widthDuration = Double( / weakself.DANMAKU_SPEED)
         
         var delay = seconds - weakself.DANMAKU_SPACE_TIME - widthDuration
         if delay >= 0 {
          delay = 0
         } else {
          if  >  {
           continue
          }
         }
    
         let danmaku = Danmaku(msg, , row, abs(delay) + )
         [row].append(danmaku)
         
         self?.performSelectorOnMainThread("sendDanmaku:", withObject: danmaku, waitUntilDone: true)
         return
        }
       }
      }
     }
     
     sleep(1000)
    } while self != nil
   }
   
   }))
 }
 
 func sendDanmaku(danmaku: Danmaku) {
  let text = "\(.user_name) : \()"
  let size = NSString(string: text).sizeWithAttributes([NSFontAttributeName : danmakuFont])
  let width = ().
  let top = 54 +  * (Int() + 10)
  let label = UILabel(frame: CGRectMake(width, CGFloat(top), , ))
  let duration = (width + ) / DANMAKU_SPEED

   = label
   = size
   = NSDate()
   = NSTimeInterval(duration)
  
   = text
   = danmakuFont
   = ()
   = ()
   = CGSizeMake(0, -1.0)
  
  (label)
  (Double(duration), delay: , options: , animations: { () -> Void in
     = -
   }) { [weak self] (Bool) -> Void in
    if !(self?.rowArray[].isEmpty ?? true) {
     self?.rowArray[].removeFirst()
    }
    ()
  }
 }

Code description:

The code controls that at most three lines can be played at the same time. If the delay of the last line is greater than the time when the barrage is running (there is already a full wait state), it will automatically cut to the next line, and wait if the maximum limit is exceeded.

*rowArray is mainly used to query the location and time of the previous barrage

*Don't forget to add () in deinit

*Note that the block of NSBlockOperation is not on the main thread

The above is the implementation code for the simple barrage function of IOS development. Friends who need to develop this function can refer to it.