Closed LiMaoAtCD closed 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() }
} `
descriptor.spriteName = "floating"
spriteName must be a chirdren classname of BarrageSprite
请问下你添加了clickAction事件后 ,点击弹幕会报错吗
@menicor 你是不是用swift 写的
import UIKit class BarrageViewController: UIViewController {
} `