gpt4 book ai didi

ios - 只运行一次 CAEmitterLayer

转载 作者:行者123 更新时间:2023-12-01 16:22:46 25 4
gpt4 key购买 nike

我只想运行一次 CAEmitterLayer,我想停止birthRate,但我做不到。我想要的是它在点击屏幕时只运行一次。我一直在尝试与代表合作,但我无法让它发挥作用。你能告诉我我的代码是否有效。

    import UIKit
import PlaygroundSupport

class Emitter {
static func get(with image: UIImage) -> CAEmitterLayer {
let emitter = CAEmitterLayer()
emitter.emitterShape = kCAEmitterLayerLine
emitter.emitterCells = generateEmitterCells(image: image)
print("emit")
emitter.setValue(0.0, forKey: "em")
return emitter
}

static func generateEmitterCells(image: UIImage) -> [CAEmitterCell] {
var cells = [CAEmitterCell]()
let cell = CAEmitterCell()
cell.contents = image.cgImage
cell.birthRate = 0.1
cell.lifetime = 20
cell.velocity = 250
cell.emissionRange = (10 * (.pi/180))
cell.scale = 0.9
cell.scaleRange = 0.3
cell.velocityRange = 100
cells.append(cell)
print("cells")
return cells
}

}

class ViewController : UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))

let view2 = UIView(frame: self.view.frame)

self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 580)
super.view.backgroundColor = UIColor.white
self.view.addSubview(view2)
}

@objc func handleTap() {
rain()
}

func rain() {
let emitter = Emitter.get(with: UIImage(named: "Group 1493")!)
emitter.emitterPosition = CGPoint(x: view.frame.width / 2, y: view.frame.height + 25)
emitter.emitterSize = CGSize(width: view.frame.width, height: 2)
self.view.layer.addSublayer(emitter)
}

}

let controller = ViewController()
PlaygroundPage.current.liveView = controller.view

最佳答案

您可以在第一次启动后将 生命周期 属性设置为 0
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
emitter.lifetime = 0
}

关于ios - 只运行一次 CAEmitterLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49909876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com