Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Iran war: US responsible for strike on Iranian school, investigation finds

    March 12, 2026

    MaxLiveProtect: eBPF-Powered Network Infrastructure Security

    March 12, 2026

    SBS Bank begins shift to cloud-native core banking platform

    March 12, 2026
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»ios – CAEmitterLayer randomly doesn’t emit particles
    iOS Development

    ios – CAEmitterLayer randomly doesn’t emit particles

    big tee tech hubBy big tee tech hubOctober 7, 20250224 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    ios – CAEmitterLayer randomly doesn’t emit particles
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    I’m implementing confetti effect with particles. I used for it CAEmitterLayer, and it works almost good, but sometimes particles are not emitted. I tried my best and debugged it (set colors to emitter layers) and I know that the layers are added. Here’s the full code:

    import UIKit
    
    final class ViewController: UIViewController {
        
        // MARK: - UI
        
        private lazy var actionButton: UIButton = { [weak self] in
            let button = UIButton(type: .system)
            button.translatesAutoresizingMaskIntoConstraints = false
            button.setTitle("Start confetti", for: .normal)
            button.setTitleColor(.white, for: .normal)
            button.backgroundColor = .systemBlue
            button.layer.cornerRadius = 16
            button.addAction(UIAction { _ in
                self?.startConfettiShow()
            }, for: .touchUpInside)
            return button
        }()
        
        private let confettiContainer: UIView = {
            let view = UIView()
            view.translatesAutoresizingMaskIntoConstraints = false
            view.backgroundColor = .white
            view.clipsToBounds = true
            return view
        }()
        
        // MARK: - Lifecycle
        
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = .gray
            
            view.addSubview(confettiContainer)
            view.addSubview(actionButton)
            
            NSLayoutConstraint.activate([
                confettiContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                confettiContainer.bottomAnchor.constraint(equalTo: actionButton.topAnchor, constant: -50),
                confettiContainer.widthAnchor.constraint(equalToConstant: 300),
                confettiContainer.heightAnchor.constraint(equalToConstant: 300),
                
                actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                actionButton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 200),
                actionButton.widthAnchor.constraint(equalToConstant: 150),
                actionButton.heightAnchor.constraint(equalToConstant: 40),
            ])
        }
        
        // MARK: - Confetti Logic
        
        private func startConfettiShow() {
            // Remove old emitters
            confettiContainer.layer.sublayers?.removeAll(where: { $0 is CAEmitterLayer })
            view.layoutIfNeeded()
            
            let xOffset: CGFloat = 90
            let emissionOffset: CGFloat = 0.1
            
            let left = makeEmitter(xOffset: -xOffset, emissionLongitudeOffset: emissionOffset)
            let right = makeEmitter(xOffset: xOffset, emissionLongitudeOffset: -emissionOffset)
            
            confettiContainer.layer.addSublayer(left)
            confettiContainer.layer.addSublayer(right)
            
            debugEmitter(left, color: .red)
            debugEmitter(right, color: .yellow)
            
            DispatchQueue.main.async {
                CATransaction.begin()
                CATransaction.setDisableActions(true)
                
                let now = CACurrentMediaTime()
                left.beginTime = now
                right.beginTime = now
                left.birthRate = 1
                right.birthRate = 1
                
                CATransaction.commit()
            }
            
            // Stop emission
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                left.birthRate = 0
                right.birthRate = 0
            }
            
            // Remove the layers 
            DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                left.removeFromSuperlayer()
                right.removeFromSuperlayer()
            }
        }
        
        private func makeEmitter(xOffset: CGFloat,
                                 emissionLongitudeOffset: CGFloat) -> CAEmitterLayer {
            let emitter = CAEmitterLayer()
            emitter.frame = confettiContainer.bounds
            emitter.emitterShape = .point
            emitter.emitterPosition = CGPoint(
                x: confettiContainer.bounds.midX + xOffset,
                y: confettiContainer.bounds.maxY - 10
            )
            emitter.birthRate = 0
            
            let cells: [CAEmitterCell] = ViewController.colors.flatMap { color in
                ViewController.shapes.map { shape in
                    let cell = CAEmitterCell()
                    cell.birthRate = 15
                    cell.lifetime = 3.5
                    cell.velocity = .random(in: 250...400)
                    cell.velocityRange = 80
                    cell.emissionLongitude = -.pi / 2 + emissionLongitudeOffset
                    cell.emissionRange = .pi / 8
                    cell.yAcceleration = 200
                    cell.spin = .random(in: -3...3)
                    cell.spinRange = 4
                    cell.scale = .random(in: 0.08...0.14)
                    cell.alphaSpeed = -0.4
                    cell.color = color.cgColor
                    cell.contents = shape
                    return cell
                }
            }
            
            emitter.emitterCells = cells
            return emitter
        }
        
        private func debugEmitter(_ emitter: CAEmitterLayer, color: UIColor) {
            emitter.borderColor = color.cgColor
            emitter.borderWidth = 1
            emitter.backgroundColor = color.withAlphaComponent(0.15).cgColor
            print("Emitter frame:", emitter.frame)
            print("Emitter position:", emitter.emitterPosition)
        }
    }
    
    // MARK: - Static resources
    
    private extension ViewController {
        static let colors: [UIColor] = [
            .systemPink, .systemYellow, .systemBlue, .systemGreen, .systemPurple
        ]
        
        static let shapes: [CGImage] = [
            CGImage.cgImageCircle(diameter: 36),
            CGImage.cgImageSquare(side: 32),
            CGImage.cgImageTriangle(size: 36),
        ].compactMap { $0 }
    }
    
    // MARK: - Shape generators
    
    extension CGImage {
        static func cgImageCircle(diameter: CGFloat) -> CGImage? {
            let size = CGSize(width: diameter, height: diameter)
            return UIGraphicsImageRenderer(size: size).image { ctx in
                UIColor.white.setFill()
                ctx.cgContext.fillEllipse(in: CGRect(origin: .zero, size: size))
            }.cgImage
        }
        
        static func cgImageSquare(side: CGFloat) -> CGImage? {
            let size = CGSize(width: side, height: side)
            return UIGraphicsImageRenderer(size: size).image { ctx in
                UIColor.white.setFill()
                ctx.cgContext.fill(CGRect(origin: .zero, size: size))
            }.cgImage
        }
        
        static func cgImageTriangle(size: CGFloat) -> CGImage? {
            let s = CGSize(width: size, height: size)
            return UIGraphicsImageRenderer(size: s).image { ctx in
                UIColor.white.setFill()
                let path = UIBezierPath()
                path.move(to: CGPoint(x: s.width / 2, y: 0))
                path.addLine(to: CGPoint(x: s.width, y: s.height))
                path.addLine(to: CGPoint(x: 0, y: s.height))
                path.close()
                path.fill()
            }.cgImage
        }
    }
    

    I’m setting beginTime and birthRate after emitters being added to view’s layer, I even used CATransaction.begin() and CATransaction.commit() (ChatGPT advice), but it didn’t worked. With all these improvements it started working better, I see less of emitters not working, but still sometimes it doesn’t work, so the element is unstable.

    Check the screenshot. There’re two emitters, but only one is emitting. Rarely they don’t emit both.

    enter image description here



    Source link

    CAEmitterLayer doesnt Emit iOS particles randomly
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    ios – SwiftUI iOS26+ Transition or Animation after TextField value entered

    March 12, 2026

    uikit – Why the title doesn’t follow the navigation inline state in iOS 26

    March 11, 2026

    ios – OS emoji keyboard causes UI freeze in chat TextField Flutter

    March 10, 2026
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Iran war: US responsible for strike on Iranian school, investigation finds

    March 12, 2026

    MaxLiveProtect: eBPF-Powered Network Infrastructure Security

    March 12, 2026

    SBS Bank begins shift to cloud-native core banking platform

    March 12, 2026

    Friday Night Baseball Returns to Apple TV on March 27 for 2026 MLB Season

    March 12, 2026
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Iran war: US responsible for strike on Iranian school, investigation finds

    March 12, 2026

    MaxLiveProtect: eBPF-Powered Network Infrastructure Security

    March 12, 2026

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2026 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.