无法在Xcode Playgrounds上运行SAConfettiView

时间:2023-01-23 18:20:44

I am trying to make the SAConfettiView framework to work in Xcode playgrounds. Though, when I use the code above the confetti animation isn't showing up.

我试图使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

What am I doing wrong?

我究竟做错了什么?

Download playground here

在这里下载游乐场

1 个解决方案

#1


2  

This is because ConfettiView.swift in the Sources folder does not load the images properly.

这是因为Sources文件夹中的ConfettiView.swift无法正确加载图像。

Replace their image(for type: ConfettiType) -> UIImage? method with this one:

替换他们的图像(对于类型: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)
}

where I use Bundle.main.url to load the confetti images properly.

我在哪里使用Bundle.main.url来正确加载五彩纸屑图像。

无法在Xcode Playgrounds上运行SAConfettiView

#1


2  

This is because ConfettiView.swift in the Sources folder does not load the images properly.

这是因为Sources文件夹中的ConfettiView.swift无法正确加载图像。

Replace their image(for type: ConfettiType) -> UIImage? method with this one:

替换他们的图像(对于类型: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)
}

where I use Bundle.main.url to load the confetti images properly.

我在哪里使用Bundle.main.url来正确加载五彩纸屑图像。

无法在Xcode Playgrounds上运行SAConfettiView