用Swift写CAEmitterLayer,如何让粒子停止发射呢?
新博娱乐手机
新博娱乐手机
当前位置 : 新博娱乐手机 >

用Swift写CAEmitterLayer,如何让粒子停止发射呢?

1.我无论如何都无法让粒子停止发射。主要问题在touchesEnded这个方法里,为什么cell.birthRate = 0这个不能让这个粒子停止发射呢?谢谢!

2.代码如下

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad {
        super.viewDidLoad
        createParticles

    }
    
    var touchPostion = CGPoint.zero {
        didSet{
            emitter.emitterPosition = touchPostion
        }
    }
    
    var emitter = CAEmitterLayer
    var cell = CAEmitterCell
    
    //产生粒子
    func createParticles {
        
        let view = UIImageViewframe: CGRectx: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height
        view.backgroundColor = UIColor.black
        
        self.view.addSubviewview
        
        emitter.emitterShape = kCAEmitterLayerLine
        emitter.emitterSize = CGSizewidth: 1, height: 1
        
        
        let flake = makeEmitterCell
        emitter.emitterCells = [flake]
        view.layer.addSublayeremitter
        print"began"
    }
    
    //开始触摸
    override func touchesBegan_ touches: Set<UITouch>, with event: UIEvent? {
        if touches.first != nil {
            touchPostion.x = touches.first?.locationin: self.view.x!
            touchPostion.y = touches.first?.locationin: self.view.y!
        }
        printtouchPostion.x , "," , touchPostion.y
        cell.birthRate = 50.0
    }
    
    //触摸移动
    override func touchesMoved_ touches: Set<UITouch>, with event: UIEvent? {
        if touches.first != nil {
            touchPostion.x = touches.first?.locationin: self.view.x!
            touchPostion.y = touches.first?.locationin: self.view.y!
        }
    }
    `
    //结束触摸
    override func touchesEnded_ touches: Set<UITouch>, with event: UIEvent? {
        //不再产生粒子
        cell.birthRate = 0.0
        print"ended"
        
    }
    `
    //关于粒子
    func makeEmitterCell -> CAEmitterCell {
        
        cell.contentsScale = 2
        cell.lifetime = 3.0
        cell.velocity = -40
        cell.xAcceleration = 20
        cell.yAcceleration = 50
        cell.zAcceleration = 30
        cell.velocityRange = 20
        cell.emissionLongitude = CGFloat.pi
        cell.emissionRange = CGFloat.pi / 4
        cell.scaleRange = 0.0
        cell.alphaSpeed = -0.4
        
        cell.contents = UIImagenamed:"Drop"?.cgImage
        print"emitting"
        
        return cell
    }

    override func viewDidDisappear_ animated: Bool {
        super.viewDidDisappearanimated
        
    }
}

栏目列表

广告位

新博娱乐手机