无法在Xcode Playgrounds上运行SAConfettiView

时间:2017-03-27 17:12:23

标签: ios swift swift-playground

我正在努力让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

我做错了什么?

Download playground here

1 个答案:

答案 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正确加载五彩纸屑图片。

enter image description here