简体   繁体   English

如何设置CAEmitterLayer背景透明?

[英]How to set CAEmitterLayer background transparent?

    var emitter = CAEmitterLayer()
    emitter.emitterPosition = CGPoint(x: self.view.frame.size.width / 2, y: -10)
    emitter.emitterShape = kCAEmitterLayerLine
    emitter.emitterSize = CGSize(width: self.view.frame.size.width, height: 2.0)
    emitter.emitterCells = generateEmitterCells()
    self.view.layer.addSublayer(emitter)

here, CAEmitterLayer covers my view... the content of self.view not visible.. 在这里,CAEmitterLayer涵盖了我的观点...... self.view的内容不可见..

Ref. 参考。 code : https://oktapodi.github.io/2017/05/08/particle-effects-in-swift-using-caemitterlayer.html 代码: https//oktapodi.github.io/2017/05/08/particle-effects-in-swift-using-caemitterlayer.html

I want to set this animation on my view. 我想在我的视图上设置这个动画。

It is working fine check output of my simulator. 它正在检查我的模拟器的精确输出。 Background images are added from storyboard and blue color is done by code. 从故事板添加背景图像,并且通过代码完成蓝色。 Still working fine. 仍然工作正常。

OUTPUT: OUTPUT: 在此输入图像描述

You can fix your problem by changing the way you add the layer right now your adding it on top of everything which sometimes hide other layers and view objects. 您可以通过更改添加图层的方式来修复问题,现在将其添加到有时隐藏其他图层和查看对象的所有内容之上。

change 更改

self.view.layer.addSublayer(emitter)

To

self.view.layer.insertSublayer(emitter, at: 0)

I don't know if I understand you correct, but if this is the effect you are looking for: 我不知道我是否理解你是对的,但如果这是你正在寻找的效果:

在此输入图像描述

Then you need to: 然后你需要:

  1. Add a "container view" for your your emitter to live in 为您的发射器添加“容器视图”
  2. Create an outlet for that view 为该视图创建一个插座
  3. set clipsToBounds to true for your container view clipsToBounds设置为true以用于容器视图

Here is my ViewController which produced the above screenshot 这是我的ViewController ,它产生了上面的截图

import UIKit


enum Colors {
    static let red = UIColor(red: 1.0, green: 0.0, blue: 77.0/255.0, alpha: 1.0)
    static let blue = UIColor.blue
    static let green = UIColor(red: 35.0/255.0 , green: 233/255, blue: 173/255.0, alpha: 1.0)
    static let yellow = UIColor(red: 1, green: 209/255, blue: 77.0/255.0, alpha: 1.0)   
}

enum Images {
    static let box = UIImage(named: "Box")!
    static let triangle = UIImage(named: "Triangle")!
    static let circle = UIImage(named: "Circle")!
    static let swirl = UIImage(named: "Spiral")!
}

class ViewController: UIViewController {
    @IBOutlet weak var emitterContainer: UIView!

    var emitter = CAEmitterLayer()

    var colors:[UIColor] = [
        Colors.red,
        Colors.blue,
        Colors.green,
        Colors.yellow
    ]

    var images:[UIImage] = [
        Images.box,
        Images.triangle,
        Images.circle,
        Images.swirl
    ]

    var velocities:[Int] = [
        100,
        90,
        150,
        200
    ]


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        view.backgroundColor = UIColor.red

        emitter.emitterPosition = CGPoint(x: emitterContainer.frame.size.width / 2, y: -10)
        emitter.emitterShape = kCAEmitterLayerLine
        emitter.emitterSize = CGSize(width: emitterContainer.frame.size.width, height: 2.0)
        emitter.emitterCells = generateEmitterCells()
        emitterContainer.layer.addSublayer(emitter)
        emitterContainer.clipsToBounds = true
    }

    private func generateEmitterCells() -> [CAEmitterCell] {
        var cells:[CAEmitterCell] = [CAEmitterCell]()
        for index in 0..<16 {
            let cell = CAEmitterCell()

            cell.birthRate = 4.0
            cell.lifetime = 14.0
            cell.lifetimeRange = 0
            cell.velocity = CGFloat(getRandomVelocity())
            cell.velocityRange = 0
            cell.emissionLongitude = CGFloat(Double.pi)
            cell.emissionRange = 0.5
            cell.spin = 3.5
            cell.spinRange = 0
            cell.color = getNextColor(i: index)
            cell.contents = getNextImage(i: index)
            cell.scaleRange = 0.25
            cell.scale = 0.1

            cells.append(cell)
        }

        return cells
    }

    private func getRandomVelocity() -> Int {
        return velocities[getRandomNumber()]
    }

    private func getRandomNumber() -> Int {
        return Int(arc4random_uniform(4))
    }

    private func getNextColor(i:Int) -> CGColor {
        if i <= 4 {
            return colors[0].cgColor
        } else if i <= 8 {
            return colors[1].cgColor
        } else if i <= 12 {
            return colors[2].cgColor
        } else {
            return colors[3].cgColor
        }
    }

    private func getNextImage(i:Int) -> CGImage {
        return images[i % 4].cgImage!
    }
}

Hope that helps you. 希望对你有所帮助。

Hello change emitterPosition X of each like below:- 你好改变每个的emitterPosition X如下: -

    let emitter1 = Emitter.getEmitter(with: #imageLiteral(resourceName: "img_ribbon_4"), directionInRadian: (180 * (.pi/180)), velocity: 50)
    emitter1.emitterPosition = CGPoint(x: self.view.frame.width / 3 , y: 0)
    emitter1.emitterSize = CGSize(width: self.view.frame.size.width, height: 2.0)
    self.view.layer.addSublayer(emitter1)


    let emitter2 = Emitter.getEmitter(with: #imageLiteral(resourceName: "img_ribbon_6"), directionInRadian: (180 * (.pi/180)), velocity: 80)
    emitter2.emitterPosition = CGPoint(x: self.view.frame.width / 2, y: 0)
    emitter2.emitterSize = CGSize(width: self.view.frame.size.width, height: 2.0)
    self.view.layer.addSublayer(emitter2)

I hope it will help you, thank you. 我希望它会对你有所帮助,谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM