我正在尝试制作 SAConfettiView在 Xcode Playground 上工作的框架。但是,当我使用上面的代码时,五彩纸屑动画没有出现。
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
我做错了什么?
最佳答案
这是因为 Sources
文件夹中的 ConfettiView.swift
没有正确加载图像。
用这个替换他们的 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
正确加载五彩纸屑图像。
关于ios - 无法在 Xcode Playgrounds 上运行 SAConfettiView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43052575/