### Playing Danmaku Animation in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README.md This code demonstrates how to start the danmaku animation playback within the `DanmakuView` by calling its `play()` method. ```Swift danmakuView.play() ``` -------------------------------- ### Install with Swift Package Manager (Swift) Source: https://github.com/qyz777/danmakukit/blob/master/README.md Add the DanmakuKit dependency to your Package.swift file. Specify the repository URL and the minimum required version to integrate it using Swift Package Manager. ```swift dependencies: [ .package(url: "https://github.com/qyz777/DanmakuKit.git", from: "1.5.0") ], targets: [ .target( name: "YourTarget", dependencies: ["DanmakuKit"]) ] ``` -------------------------------- ### Installing DanmakuKit via Swift Package Manager in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Swift code snippet is used in a Package.swift file to declare the DanmakuKit dependency for a project using Swift Package Manager. It specifies the Git repository URL and the minimum version (`from: "1.5.0"`), and links the dependency to a target. ```Swift dependencies: [ .package(url: "https://github.com/qyz777/DanmakuKit.git", from: "1.5.0") ], targets: [ .target( name: "YourTarget", dependencies: ["DanmakuKit"]) ] ``` -------------------------------- ### Install with Cocoapods (Ruby) Source: https://github.com/qyz777/danmakukit/blob/master/README.md Add the DanmakuKit pod to your Podfile to integrate it using Cocoapods. Specify the desired version using the '~>' operator to allow patch updates. ```ruby pod 'DanmakuKit', '~> 1.5.0' ``` -------------------------------- ### Starting Danmaku Playback in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Swift code calls the `play()` method on a `DanmakuView` instance. This action initiates the animation and display of the danmaku cells that have been added to the view. ```Swift danmakuView.play() ``` -------------------------------- ### Installing DanmakuKit via CocoaPods in Ruby Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Ruby code snippet is used in a Podfile to declare the DanmakuKit dependency for a project using CocoaPods. It specifies the library name and a version constraint (`~> 1.5.0`) to ensure compatibility. ```Ruby pod 'DanmakuKit', '~> 1.5.0' ``` -------------------------------- ### Creating DanmakuView and Shooting Danmaku in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Swift code snippet shows how to initialize a `DanmakuView` instance. It then creates an instance of the custom `DanmakuTextCellModel`, sets its properties like `displayTime`, `text`, and `type`, calculates its size, and finally calls the `shoot` method on the `danmakuView` to add the danmaku to the display queue. ```Swift let danmakuView = DanmakuView(frame: CGRect(x: 0, y: 0, width: 350, height: 250)) view.addSubview(danmakuView) let cellModel = DanmakuTextCellModel(json: nil) cellModel.displayTime = displayTime cellModel.text = contents[index] cellModel.identifier = String(arc4random()) cellModel.calculateSize() cellModel.type = .floating danmakuView.shoot(danmaku: cellModel) ``` -------------------------------- ### Displaying Danmaku in DanmakuView in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README.md This snippet shows how to initialize a `DanmakuView`, create an instance of a custom cell model (`DanmakuTextCellModel`), configure its properties, and add it to the view for display using the `shoot` method. ```Swift let danmakuView = DanmakuView(frame: CGRect(x: 0, y: 0, width: 350, height: 250)) view.addSubview(danmakuView) let cellModel = DanmakuTextCellModel(json: nil) cellModel.displayTime = displayTime cellModel.text = contents[index] cellModel.identifier = String(arc4random()) cellModel.calculateSize() cellModel.type = .floating danmakuView.shoot(danmaku: cellModel) ``` -------------------------------- ### Implementing Danmaku Cell Model in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README.md This snippet shows how to create a custom danmaku cell model by inheriting from `DanmakuCellModel`. It includes properties for text, font, display time, type, and methods for calculating size and checking equality. ```Swift class DanmakuTextCellModel: DanmakuCellModel { var identifier = "" var text = "" var font = UIFont.systemFont(ofSize: 15) var offsetTime: TimeInterval = 0 var cellClass: DanmakuCell.Type { return DanmakuTextCell.self } var size: CGSize = .zero var track: UInt? var displayTime: Double = 8 var type: DanmakuCellType = .floating var isPause = false func calculateSize() { size = NSString(string: text).boundingRect(with: CGSize(width: CGFloat(Float.infinity ), height: 20), options: [.usesFontLeading, .usesLineFragmentOrigin], attributes: [.font: font], context: nil).size } func isEqual(to cellModel: DanmakuCellModel) -> Bool { return identifier == cellModel.identifier } } ``` -------------------------------- ### Implementing Danmaku Cell View in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README.md This code demonstrates how to create a custom danmaku cell view by inheriting from `DanmakuCell`. It overrides the `displaying` method to perform custom drawing using Core Graphics, noting that this method may not be called on the main thread. ```Swift class DanmakuTextCell: DanmakuCell { required init(frame: CGRect) { super.init(frame: frame) backgroundColor = .clear } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func displaying(_ context: CGContext, _ size: CGSize, _ isCancelled: Bool) { guard let model = model as? DanmakuTextCellModel else { return } let text = NSString(string: model.text) context.setLineWidth(1) context.setLineJoin(.round) context.setStrokeColor(UIColor.white.cgColor) context.saveGState() context.setTextDrawingMode(.stroke) let attributes: [NSAttributedString.Key: Any] = [.font: model.font, .foregroundColor: UIColor.white] text.draw(at: .zero, withAttributes: attributes) context.restoreGState() context.setTextDrawingMode(.fill) text.draw(at: .zero, withAttributes: attributes) } } ``` -------------------------------- ### Stopping and Cleaning Up DanmakuView in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README.md This code demonstrates how to stop the danmaku animation and release associated resources within the `DanmakuView` by calling its `stop()` method. This method is automatically called when the view is destroyed. ```Swift danmakuView.stop() ``` -------------------------------- ### Defining Custom Cell Model for Text Danmaku in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Swift class defines a custom cell model, `DanmakuTextCellModel`, inheriting from `DanmakuCellModel`. It includes properties like `text`, `font`, `offsetTime`, and `displayTime` to store data for a text-based danmaku. It also implements the `calculateSize` method to determine the size of the danmaku based on its text and font, and the `isEqual` method for comparison. ```Swift class DanmakuTextCellModel: DanmakuCellModel { var identifier = "" var text = "" var font = UIFont.systemFont(ofSize: 15) var offsetTime: TimeInterval = 0 var cellClass: DanmakuCell.Type { return DanmakuTextCell.self } var size: CGSize = .zero var track: UInt? var displayTime: Double = 8 var type: DanmakuCellType = .floating var isPause = false func calculateSize() { size = NSString(string: text).boundingRect(with: CGSize(width: CGFloat(Float.infinity ), height: 20), options: [.usesFontLeading, .usesLineFragmentOrigin], attributes: [.font: font], context: nil).size } func isEqual(to cellModel: DanmakuCellModel) -> Bool { return identifier == cellModel.identifier } } ``` -------------------------------- ### Pausing Danmaku Animation in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README.md This code demonstrates how to pause the danmaku animation playback within the `DanmakuView` by calling its `pause()` method. ```Swift danmakuView.pause() ``` -------------------------------- ### Implementing Custom Cell View for Text Danmaku in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Swift class defines a custom cell view, `DanmakuTextCell`, inheriting from `DanmakuCell`. It overrides the `displaying` method to handle the drawing of the danmaku content using `CGContext`. This method is called on a background thread, requiring thread-safe drawing operations. It draws the text with a stroke and fill effect. ```Swift class DanmakuTextCell: DanmakuCell { required init(frame: CGRect) { super.init(frame: frame) backgroundColor = .clear } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func displaying(_ context: CGContext, _ size: CGSize, _ isCancelled: Bool) { guard let model = model as? DanmakuTextCellModel else { return } let text = NSString(string: model.text) context.setLineWidth(1) context.setLineJoin(.round) context.setStrokeColor(UIColor.white.cgColor) context.saveGState() context.setTextDrawingMode(.stroke) let attributes: [NSAttributedString.Key: Any] = [.font: model.font, .foregroundColor: UIColor.white] text.draw(at: .zero, withAttributes: attributes) context.restoreGState() context.setTextDrawingMode(.fill) text.draw(at: .zero, withAttributes: attributes) } } ``` -------------------------------- ### Stopping Danmaku Playback and Cleaning Resources in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Swift code calls the `stop()` method on a `DanmakuView` instance. This action stops the danmaku playback, removes all displayed danmaku, and cleans up associated resources. It is typically called when the view is being deallocated. ```Swift danmakuView.stop() ``` -------------------------------- ### Pausing Danmaku Playback in Swift Source: https://github.com/qyz777/danmakukit/blob/master/README_CN.md This Swift code calls the `pause()` method on a `DanmakuView` instance. This action temporarily halts the animation and movement of the danmaku cells currently displayed. ```Swift danmakuView.pause() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.