簡體   English   中英

EmitterCells 未顯示在 CAEmitterLayer 中

[英]EmitterCells not showing in CAEmitterLayer

我似乎無法獲得 CAEmitterLayer 顯示 CAEmitterCells。 我在 Storyboard 中添加了 ConfettiEmitterView,並且視圖按預期顯示為紅色背景。 但是細胞沒有顯示出來。 也沒有錯誤或警告。

編碼:

import UIKit

class ConfettiEmitterView: UIView {

override class var layerClass:AnyClass {
    return CAEmitterLayer.self
}

func makeEmmiterCell(color:UIColor, velocity:CGFloat, scale:CGFloat)-> CAEmitterCell {
    let cell = CAEmitterCell()
    cell.birthRate = 10
    cell.lifetime = 20.0
    cell.lifetimeRange = 0
    cell.velocity = velocity
    cell.velocityRange = velocity / 4
    cell.emissionLongitude = .pi
    cell.emissionRange = .pi / 8
    cell.scale = scale
    cell.scaleRange = scale / 3
    cell.contents = roundImage(with: .yellow)
    return cell
}

override func layoutSubviews() {
    let emitter = self.layer as! CAEmitterLayer
    emitter.frame = self.bounds
    emitter.masksToBounds = true
    emitter.emitterShape = .line
    emitter.emitterPosition = CGPoint(x: bounds.midX, y: 0)
    emitter.emitterSize = CGSize(width: bounds.size.width, height: 1)
    
    emitter.backgroundColor = UIColor.red.cgColor // Setting the background to red
    
    let near = makeEmmiterCell(color: UIColor(white: 1, alpha: 1), velocity: 100, scale: 0.3)
    let middle = makeEmmiterCell(color: UIColor(white: 1, alpha: 0.66), velocity: 80, scale: 0.2)
    let far = makeEmmiterCell(color: UIColor(white: 1, alpha: 0.33), velocity: 60, scale: 0.1)
    
    emitter.emitterCells = [near, middle, far]
}

func roundImage(with color: UIColor) -> UIImage {
        let rect = CGRect(origin: .zero, size: CGSize(width: 12.0, height: 12.0))
        return UIGraphicsImageRenderer(size: rect.size).image { context in
            context.cgContext.setFillColor(color.cgColor)
            context.cgContext.addPath(UIBezierPath(ovalIn: rect).cgPath)
            context.cgContext.fillPath()
        }
    }

}

將 ConfettiEmitterView 類型的視圖添加到 Storyboard 中的 UIViewController 應該足以重新創建問題。

改變

cell.contents = roundImage(with: .yellow)

cell.contents = roundImage(with: .yellow).cgImage

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM