我正在尝试设置CAEmitter。所有这些都有效,但我希望在一秒钟后将birthRate更改为零,以便动画停止并且不会产生新的粒子。这是我目前的代码:
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)
的方法。它会被添加到图层中吗?
谢谢你的帮助。
答案 0 :(得分:0)
您应该将emitterLayer的birthRate设置为零。
或者,您也可以使用emitterLayer的生命周期为零。