2016-07-22 61 views
0

我想創建一些使用粒子的活動圖形,在這裏我發射粒子並更改發射器位置。使用粒子創建一個特殊的圖形?

問題是我無法找到一種方式來將粒子以直線水平排列。他們似乎有一些雪(重力和旋轉)的影響。

有沒有一種方法來繪製一個像從左到右的線的粒子?

func createParticles() 
{ 
    let particleEmitter = CAEmitterLayer() 

    particleEmitter.emitterPosition = CGPoint(x: self.center.x, y: 100) 
    particleEmitter.emitterShape = kCAEmitterLayerPoint 
    particleEmitter.emitterSize = CGSize(width: self.frame.size.width, height: 1) 

    let red = makeEmitterCellWithColor(Globals.sharedInstance.palleteRed) 
    let green = makeEmitterCellWithColor(Globals.sharedInstance.palleteRed) 
    let blue = makeEmitterCellWithColor(Globals.sharedInstance.palleteRed) 

    particleEmitter.emitterCells = [red, green, blue] 

    self.layer.addSublayer(particleEmitter) 

} 



func makeEmitterCellWithColor(color: UIColor) -> CAEmitterCell { 
    let cell = CAEmitterCell() 
    cell.birthRate = 1 
    cell.lifetime = 6.0 
    cell.lifetimeRange = 0 
    cell.color = color.CGColor 
    cell.velocity = 100 
    cell.velocityRange = 220 
    cell.emissionLongitude = CGFloat(M_PI) 
    cell.emissionRange = CGFloat(M_PI_4) 
    cell.spin = 0 
    cell.spinRange = 0 
    cell.scaleRange = 0.15 
    cell.scaleSpeed = -0.05 

    cell.contents = UIImage(named: "particle")?.CGImage 
    return cell 
} 

回答

0

我也challange類似的問題這段代碼幫助我克服this.I只是試圖減少雪的影響發射範圍。

 let em : CAEmitterLayer = self.view.layer as! CAEmitterLayer 

     em.renderMode = kCAEmitterLayerOldestFirst 
     em.emitterShape = kCAEmitterLayerLine 
     em.emitterMode = kCAEmitterLayerSurface 
     em.emitterSize = CGSize(width: 10, height: 10) 
     em.beginTime = CACurrentMediaTime() 

     em.emitterPosition = CGPointMake(self.view.frame.width/2, self.view.frame.height/2) 

     let fireCell = CAEmitterCell() 
     fireCell.contents = UIImage(named: "pink.png")!.CGImage 
     fireCell.contentsRect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0) 
     fireCell.birthRate = 5.0 
     fireCell.lifetime = 5 
     fireCell.velocity = 200 
     fireCell.scale = 1 

     fireCell.color = UIColor.blueColor().CGColor 

     // emissions angle stuff 
     fireCell.emissionLongitude = CGFloat(M_PI/2) 
     fireCell.emissionRange = CGFloat(M_PI_2/10.0) 

     em.emitterCells = [fireCell]