ios - 无法在 Xcode Playgrounds 上运行 SAConfettiView

标签 ios swift swift-playground

我正在尝试制作 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

我做错了什么?

Download playground here

最佳答案

这是因为 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 正确加载五彩纸屑图像。

enter image description here

关于ios - 无法在 Xcode Playgrounds 上运行 SAConfettiView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43052575/

相关文章:

ios - CGPath 的错误比例 - Swift

swift - ViewController 之间传递数据取决于 UIButton 的标签

ios - 更新 Firestore 数据库导致 iOS 崩溃

iphone - 改进 iPhone 中的 GridView 显示性能

ios - 在 Collection View 单元格中设置标签值时发现 nil

swift - {{{…}}} 在 Swift 中是什么意思?

Swift 泛型控制台输出

swift - 我如何在 Xcode 11 Playground 中使用图像?

ios - 用于更改 UILabel 值的 UIAlertView 文本字段

swift - 无法加载和显示解析数据