无法在 Xcode 游乐场上 运行 SAConfettiView
Unable to run SAConfettiView on Xcode Playgrounds
我正在尝试使 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
我做错了什么?
这是因为 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
正确加载五彩纸屑图像。
我正在尝试使 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
我做错了什么?
这是因为 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
正确加载五彩纸屑图像。