0

当我添加多个对象时,我必须一一写如下。当我想添加 2 个或更多对象时,我总是必须复制和粘贴。我不希望这样,我尝试用 forLoop 来做,但我只看到最后添加的任何对象。 在此处输入图像描述

循环

我写了一个函数。ForLoop 的工作量与数组中的图像数量一样多。

func prepareParticeCell(particles: [String]) -> CAEmitterCell {
    
    let particleCell = CAEmitterCell()
    
    for particleItem in particles {
        
        let particle = UIImage(named: particleItem)?.cgImage
        particleCell.contents = particle
        particleCell.name = "Square"
        particleCell.birthRate = 5
        particleCell.lifetime = 74.5
        particleCell.velocityRange = 0.0
        particleCell.velocity = 79.0
        particleCell.xAcceleration = 0.0
        particleCell.yAcceleration = 0.0
        particleCell.emissionLatitude = 1*6.0 * (.pi / 180)
        particleCell.emissionLongitude = -105.0 * (.pi / 180)
        particleCell.emissionRange = 360.0 * (.pi / 180.0)
        particleCell.spin = -65.6 * (.pi / 180.0)
        particleCell.spinRange = 314.2 * (.pi / 180.0)
        particleCell.scale = 0.043
        particleCell.scaleRange = 0.7
        particleCell.scaleSpeed = 0.02
        particleCell.alphaRange = 0.0
        particleCell.alphaSpeed = 0.47
        particleCell.color = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
    }
    return particleCell
}

使用功能

 let host = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        
        let particlesLayer = CAEmitterLayer()
        particlesLayer.frame = CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        host.layer.insertSublayer(particlesLayer, at: 0)
        host.layer.masksToBounds = true
        host.insetsLayoutMarginsFromSafeArea = false
        particlesLayer.backgroundColor = .none
        particlesLayer.emitterShape = .circle
        particlesLayer.emitterPosition = CGPoint(x: 509.4, y: 707.7)
        particlesLayer.emitterSize = CGSize(width: 1648.0, height: 1112.0)
        particlesLayer.emitterMode = .outline
        particlesLayer.renderMode = .backToFront
        
        particlesLayer.emitterCells = [prepareParticeCell(particles: particleImages)] // here
        return host
4

1 回答 1

0

您必须返回一个数组,我为您提供了解决此问题的2种方法,一种与for另一种与map. 两者都在做同样的事情。


func prepareParticeCellV1(particles: [String]) -> [CAEmitterCell] {
    
    var arrayParticleCell: [CAEmitterCell] = [CAEmitterCell]()  // <<: Here
    
    for particleItem in particles {
        
        let particleCell: CAEmitterCell = CAEmitterCell()
        
        particleCell.contents = UIImage(named: particleItem)?.cgImage
        particleCell.name = "Square"
        particleCell.birthRate = 5
        particleCell.lifetime = 74.5
        particleCell.velocityRange = 0.0
        particleCell.velocity = 79.0
        particleCell.xAcceleration = 0.0
        particleCell.yAcceleration = 0.0
        particleCell.emissionLatitude = 1*6.0 * (.pi / 180)
        particleCell.emissionLongitude = -105.0 * (.pi / 180)
        particleCell.emissionRange = 360.0 * (.pi / 180.0)
        particleCell.spin = -65.6 * (.pi / 180.0)
        particleCell.spinRange = 314.2 * (.pi / 180.0)
        particleCell.scale = 0.043
        particleCell.scaleRange = 0.7
        particleCell.scaleSpeed = 0.02
        particleCell.alphaRange = 0.0
        particleCell.alphaSpeed = 0.47
        particleCell.color = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
        
        arrayParticleCell.append(particleCell)

    }

    return arrayParticleCell
}

func prepareParticeCellV2(particles: [String]) -> [CAEmitterCell] {
    
    return particles.map({ item in
        
        let particleCell: CAEmitterCell = CAEmitterCell()
        
        particleCell.contents = UIImage(named: item)?.cgImage
        particleCell.name = "Square"
        particleCell.birthRate = 5
        particleCell.lifetime = 74.5
        particleCell.velocityRange = 0.0
        particleCell.velocity = 79.0
        particleCell.xAcceleration = 0.0
        particleCell.yAcceleration = 0.0
        particleCell.emissionLatitude = 1*6.0 * (.pi / 180)
        particleCell.emissionLongitude = -105.0 * (.pi / 180)
        particleCell.emissionRange = 360.0 * (.pi / 180.0)
        particleCell.spin = -65.6 * (.pi / 180.0)
        particleCell.spinRange = 314.2 * (.pi / 180.0)
        particleCell.scale = 0.043
        particleCell.scaleRange = 0.7
        particleCell.scaleSpeed = 0.02
        particleCell.alphaRange = 0.0
        particleCell.alphaSpeed = 0.47
        particleCell.color = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
        
        return particleCell
        
    })
    
}
于 2021-03-17T18:36:25.117 回答