unash / BarrageRenderer

一个 iOS 上的弹幕渲染库.
https://github.com/unash/BarrageRenderer
MIT License
2.2k stars 453 forks source link

iOS10 swift3.0 使用pod 安装 按照demo写了一段代码,但是没有生成弹幕,log 显示弹幕数量一直为0,代码如下 #39

Closed LiMaoAtCD closed 8 years ago

LiMaoAtCD commented 8 years ago

import UIKit class BarrageViewController: UIViewController {

let renderer = BarrageRenderer.init()
var timer: Timer?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    view.addSubview(renderer.view)
    renderer.canvasMargin = UIEdgeInsetsMake(10, 10, 10, 10)
    renderer.view.isUserInteractionEnabled = true
    view.sendSubview(toBack: renderer.view)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    renderer.start()
    let safeObj = NSSafeObject.init(object: self, with: #selector(BarrageViewController.autoSendBarrage))
    if #available(iOS 10.0, *) {
        timer = Timer.scheduledTimer(timeInterval: 0.5, target: safeObj!, selector: #selector(NSSafeObject.excute), userInfo: nil, repeats: true)
    } else {
        // Fallback on earlier versions

    }

}

func autoSendBarrage() {
    let spriteNumber = renderer.spritesNumber(withName: nil)
    print("当前屏幕的弹幕数量: \(spriteNumber)")
    if spriteNumber < 100 {
        renderer.receive(walkTextSpriteDescription(withDirection: BarrageWalkDirection.R2L, side: BarrageWalkSide.left))
        renderer.receive(floatingTextSpriteDescriptor(withDirection: BarrageWalkDirection.B2T, side: BarrageFloatSide.center))
    }
}

func walkTextSpriteDescription(withDirection direction: BarrageWalkDirection, side: BarrageWalkSide) -> BarrageDescriptor {
    let descriptor = BarrageDescriptor()
    descriptor.spriteName = "walk"
    descriptor.params["attributedText"] = NSAttributedString.init(string: "过场文字: \(index = index + 1)", attributes: [NSForegroundColorAttributeName: UIColor.red, NSFontAttributeName: UIFont.systemFont(ofSize: 20)])

    descriptor.params["speed"] = NSNumber.init(value: 1.0)
    descriptor.params["direction"] = NSNumber.init(value: direction.rawValue)
    descriptor.params["side"] = NSNumber.init(value: side.rawValue)
    descriptor.params["clickAction"] = {
        print("click")
    }
    return descriptor
}

var index = 0
func floatingTextSpriteDescriptor(withDirection direction: BarrageWalkDirection, side: BarrageFloatSide) -> BarrageDescriptor {
    let descriptor = BarrageDescriptor()
    descriptor.spriteName = "floating"
    descriptor.params["text"] = "浮动文字: \(index = index + 1)"
    descriptor.params["textColor"] = UIColor.red
    descriptor.params["duration"] = NSNumber.init(value: 3)
    descriptor.params["fadeInTime"] = NSNumber.init(value: 1)

    descriptor.params["fadeOutTime"] = NSNumber.init(value: 1)
    descriptor.params["direction"] = NSNumber.init(value: direction.rawValue)
    descriptor.params["side"] = NSNumber.init(value: side.rawValue)
    return descriptor

}
deinit {
    renderer.stop()
}

} `

LiMaoAtCD commented 8 years ago
descriptor.spriteName = "floating"

spriteName must be a chirdren classname of BarrageSprite

menicor commented 7 years ago

请问下你添加了clickAction事件后 ,点击弹幕会报错吗

LiMaoAtCD commented 7 years ago

@menicor 你是不是用swift 写的