0
我正在嘗試設置CAEmitter。所有這些都有效,但我想在一秒鐘之後將birthRate更改爲零,以便動畫停止並且不會產生新的粒子。這裏是我目前的代碼:無法更改CAEmitter的屬性SWIFT
import PlaygroundSupport
import UIKit
let view = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let emitterLayer = CAEmitterLayer()
func degreesToRadians(degrees: Double) -> CGFloat {
return CGFloat(degrees * M_PI/180.0)
}
emitterLayer.emitterPosition = CGPoint(x: 320, y: 150)
let cell = CAEmitterCell()
cell.birthRate = 100
cell.lifetime = 1
cell.velocity = 300
cell.scale = 0.1
cell.contents = #imageLiteral(resourceName: "Lock copy.png").cgImage
let zeroDegreesInRadians = degreesToRadians(degrees: 0.0)
cell.spin = degreesToRadians(degrees: 130.0)
cell.spinRange = zeroDegreesInRadians
cell.emissionRange = degreesToRadians(degrees: 100)
cell.xAcceleration = -800.0
cell.yAcceleration = 1000.0
emitterLayer.emitterCells = [cell]
let delayTime = DispatchTime.now() + Double(Int64(1.0 * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: delayTime, execute:{
cell.birthRate = 0
})
view.layer.addSublayer(emitterLayer)
let page = PlaygroundPage.current
page.needsIndefiniteExecution = true
page.liveView = view
我看不到任何效果,爲什麼我的代碼不工作?另外我將如何添加一個CABasicAnimation來改變屬性。 CAEmitterCell沒有像.add(anim: CAAnimation, forKey: String)
這樣的方法。它會被添加到圖層嗎? 感謝您的幫助。
如何使用定時器呢?每次計時器啓動時,您可以減少一定的出生率 – JoRa
爲什麼我的代碼不會一次性降低出生率? – Lenny1357
你的代碼是否會降低實際被調用的出生率?你初始化'delayTime'的方式似乎很簡單。 –