• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

EmitterCells在CAEmitterLayer上不显示

iOS cps12345 5个月前 (12-02) 82次浏览 0个评论

我似乎看不到CAEmitterLayer显示CAEmitterCells。我在StoryBoard中添加了ConfettiEmitterView,并且该视图以预期的红色背景显示。但是cell没有显示。也没有错误或警告。

代码:

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()
        }
    }

}

解决

修改

cell.contents = roundImage(with: .yellow)

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

 

喜欢 (0)

您必须 登录 才能发表评论!