首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAEmitterLayer中未显示EmitterCells

CAEmitterLayer中未显示EmitterCells
EN

Stack Overflow用户
提问于 2020-12-02 07:04:00
回答 1查看 62关注 0票数 0

我好像拿不到CAEmitterLayer display CAEmitterCells。我在故事板中添加了ConfettiEmitterView,视图按照预期以红色背景显示。但是细胞没有显示出来。也没有错误或警告。

代码:

代码语言:javascript
复制
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()
        }
    }

}

在故事板中的UIViewController中添加ConfettiEmitterView类型的视图应该足以重新创建问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-02 11:01:07

变化

代码语言:javascript
复制
cell.contents = roundImage(with: .yellow)

代码语言:javascript
复制
cell.contents = roundImage(with: .yellow).cgImage
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65099891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档