我正在努力让SAConfettiView框架在Xcode游乐场中运行。但是,当我使用上面的代码时,五彩纸屑动画并没有出现。
import UIKit
import PlaygroundSupport
var view = UIView(frame: UIScreen.main.bounds)
var confettiView: SAConfettiView!
confettiView = SAConfettiView(frame: view.bounds)
confettiView.type = .Star
view.addSubview(confettiView)
confettiView.startConfetti()
view.backgroundColor = .red
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
我做错了什么?
答案 0 :(得分:2)
这是因为ConfettiView.swift
文件夹中的Sources
无法正确加载图片。
用这个方法替换他们的image(for type: ConfettiType) -> UIImage?
方法:
func image(for type: ConfettiType) -> UIImage? {
var fileName: String
switch type {
case .Confetti:
fileName = "confetti"
case .Triangle:
fileName = "triangle"
case .Star:
fileName = "star"
case .Diamond:
fileName = "diamond"
case let .Image(customImage):
return customImage
}
guard let url = Bundle.main.url(forResource: fileName, withExtension: "png"),
let data = try? Data(contentsOf: url) else
{
return nil
}
return UIImage(data: data)
}
我使用Bundle.main.url
正确加载五彩纸屑图片。