kCAEmitterLayerLine
始终是一条厚度为零的水平线。仅emitterSize.width
使用。
您可以通过将发射器层的变换设置为旋转来垂直使用线条形状。游乐场示例:
import UIKit
import PlaygroundSupport
class VerticalEmitterView: UIView {
let emitter = CAEmitterLayer()
override func layoutSubviews() {
super.layoutSubviews()
let bounds = self.bounds
emitter.position = CGPoint(x: bounds.midX, y: bounds.midY)
emitter.bounds = CGRect(x: 0, y: 0, width: bounds.size.height, height: bounds.size.width)
emitter.emitterPosition = CGPoint(x: bounds.width / 2, y: -50)
emitter.emitterSize = CGSize(width: emitter.bounds.size.width, height: 0)
if emitter.superlayer != self.layer {
emitter.setAffineTransform(CGAffineTransform(rotationAngle: -.pi / 2))
emitter.emitterShape = kCAEmitterLayerLine
let cell = CAEmitterCell()
cell.birthRate = 100
cell.lifetime = 10
cell.velocity = 50
cell.emissionLongitude = .pi
cell.color = UIColor.red.cgColor
let particleSize = CGSize(width: 5, height: 5)
let image = UIGraphicsImageRenderer(size: particleSize).image(actions: { (rc) in
UIColor.white.setFill()
let gc = UIGraphicsGetCurrentContext()
UIBezierPath(ovalIn: CGRect(origin: .zero, size: particleSize)).fill()
})
cell.contents = image.cgImage
emitter.emitterCells = [cell]
layer.addSublayer(emitter)
}
}
}
let rootView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
rootView.backgroundColor = .white
let emitterView = VerticalEmitterView(frame: rootView.bounds)
rootView.addSubview(emitterView)
PlaygroundPage.current.liveView = rootView
结果:
(不连续是因为这个 GIF 是一个一秒钟的循环。)