2017-03-27 62 views
1

我正在嘗試使SAConfettiView框架在Xcode遊樂場中工作。雖然,當我使用上面的代碼五彩紙屑動畫沒有出現。無法在Xcode Playgrounds上運行SAConfettiView

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

回答

2

這是因爲ConfettiView.swiftSources文件夾不正確加載圖像。

與這一個更換他們的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