-2

hello I have a question about emissionLongitude and latitude i don't know what are they used for. I currently study a tutorial book about animations and I am on CAEmitterLayer right now and i have stopped studying because I don't know about these two things. I will appreciate you help. thank you.

Bako Abdullah
  • 75
  • 2
  • 8
  • 1
    Have you done any searching yet? Go to google (or your favorite search engine) and search for `caemitterlayer longitude and latitude` ... lots of results, with documentation, tutorials, examples, etc. – DonMag Aug 25 '20 at 14:06
  • of course thats why I asked – Bako Abdullah Aug 25 '20 at 16:26
  • 1
    Not sure how you could do that search, and read through some articles, and still not understand what Longitude and Latitude are used for. Try this one (I have no relation to it, just found it quickly via search): https://medium.com/@dkw5877/fun-with-particle-emitters-dcc438c4639f – DonMag Aug 25 '20 at 16:30
  • thank you for your help it was helpful but still not what I really wanted to get. but I really appreciate your help – Bako Abdullah Aug 26 '20 at 11:47
  • "I don't know" is not a question. The documentation tells you, and lots of tutorials and examples show you, what they are for. – matt Aug 26 '20 at 15:04

1 Answers1

2

Frequently, the best way to learn is by trying. I'd suggest you follow the link to the source code at the end of that article (here), download the project, and play around with the code.

Based on that article, here is a quick, simple example:

class EmmitterTestViewController: UIViewController {

    let eView = BasicParticleView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // create a 12x12 round red image
        let img = roundImage(with: .red)
        eView.particleImage = img
        
        eView.translatesAutoresizingMaskIntoConstraints = false
        eView.backgroundColor = .cyan
        
        view.addSubview(eView)
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            eView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
            eView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            eView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            eView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
        ])
    }
    
    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()
        }
    }

}

class BasicParticleView:UIView {
    
    var particleImage:UIImage?
    
    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.emissionRange = .pi / 8
        cell.scale = scale
        cell.scaleRange = scale / 3
        
        cell.contents = particleImage?.cgImage

        // emissionLongitude - direction of particles on x/y plane
        //  .pi * 0.0 == up
        //  .pi * 1.0 == down
        //  .pi * 0.5 == left-to-right
        //  .pi * 1.5 == right-to-left
        //  .pi * 0.25 == angle-up-right
        cell.emissionLongitude = .pi * 0.0
        
        return cell
    }
    
    override func layoutSubviews() {
        let emitter = self.layer as! CAEmitterLayer
        emitter.masksToBounds = true
        emitter.emitterShape = .line
        
        emitter.emitterPosition = CGPoint(x: bounds.midX, y: bounds.midY)
        emitter.emitterSize = CGSize(width: 1, height: 1)

        let cell = makeEmmiterCell(color: UIColor(white: 1, alpha: 1), velocity: 100, scale: 0.3)
        emitter.emitterCells = [cell]
    }
    
}

This will create a cyan view (inset by 20-pts on each side to make it easy to see), and will generate round, red particles. Note in the comments how you can use .emissionLongitude to change the direction.

Here's how it looks with .emissionLongitude = 0 -- particles moving "up":

enter image description here

Here's how it looks with .emissionLongitude = .pi -- particles moving "down":

enter image description here

And with .emissionLongitude = .pi * 0.25 -- particles moving "up-right":

enter image description here

Play with the settings... read the docs... read articles / tutorials... examine example code... Learn as you go :)

DonMag
  • 69,424
  • 5
  • 50
  • 86