使用 forEach 和 SwiftUI

问题描述

当我添加多个对象时,我必须一一写如下。当我想添加 2 个或更多对象时,我总是必须复制和粘贴。我不想要那个,我尝试用 forLoop 来做,但我只看到最后添加的任何对象。

enter image description here

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,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

解决方法

您必须返回一个数组,我给了您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,alpha: 1.0).cgColor
        
        return particleCell
        
    })
    
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...