GithubHelp home page GithubHelp logo

sunjinshuai / danmakukit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from qyz777/danmakukit

0.0 0.0 0.0 28.88 MB

高性能弹幕框架 DanmakuKit is a high performance library that provides the basic functions of danmaku.

Home Page: https://github.com/qyz777/DanmakuKit

License: MIT License

Ruby 1.86% Swift 98.14%

danmakukit's Introduction

DanmakuKit

Version License Platform

DanmakuKit(中文)

Introduction

DanmakuKit is a high performance library that provides the basic functions of danmaku. It provides a set of processes that allow you to generate the danmaku cell via cellModel, and each danmaku can be drawn either synchronously or asynchronously.

中文博客

As shown in the GIF below, DanmakuKit offers three types of danmaku launch: floating, top and bottom.

Demo_0

Demo_1

Supported features

  • Speed adjustment
  • Track height adjustment
  • Display area adjustment
  • Click callback
  • Support to pause or play a single danmaku
  • Provides property to specify whether danmaku can overlap
  • Support to disable danmaku for different types of tracks
  • Support for setting progress property to render the danmaku immediately on the view
  • Support to clean up all danmaku
  • Support Gif Danmaku

Use Guide

Draw Danmaku

  1. Implement a model to inherit from the DanmakuCellModel. In this model you need to add your own properties and methods to draw the danmaku.
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
    }
}
  1. Implement a View inherited from DanmakuCell. Then you need to override the displaying method, and use CGContext draw your danmaku. It is important to note when call displaying method was not in the main thread, so you need to consider multithreading.
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)
    }
}
  1. Pass the DanmakuCellModel to the DanmakuView to display danmaku.
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)

Play Danmaku

Call play() method to start display danmaku.

danmakuView.play()

Call pause() method to pause the play of danmaku.

danmakuView.pause()

Call stop() method to stop display danmaku and clean up resource. The memory associated with DanmakuKit is not cleaned up until this method is called. This method will be invoked when DanmakuView destroyed.

danmakuView.stop()

Gif Danmaku

If you want to display GIF on a danmaku, then import the Gif subspec and use the DanmakuGifCell and DanmakuGifCellModel.

Other Features

See the Example project for more features.

Requirements

swift 5.0+

iOS 9.0+

Installation

DanmakuKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'DanmakuKit', '~> 1.3.0'

License

DanmakuKit is available under the MIT license. See the LICENSE file for more info.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.