swiftUI 粒子效果

2021-11-20  本文已影响0人  王勋才
//
//  CALayerView.swift
//  SwiftUIPL
//
//  Created by wangxuncai on 2021/11/20.
//

import SwiftUI

struct CALayerView:UIViewRepresentable{
    func makeUIView(context: Context) -> UIView {
        let view = UIView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 300, height: 800)))
        let emitterLayer = CAEmitterLayer()
            
        emitterLayer.emitterPosition = CGPoint(x: Int.random(in: 0...320), y: Int.random(in: 0...320))
      
        let cell = CAEmitterCell()
       
        cell.birthRate = 100
        cell.lifetime = 10
        cell.lifetimeRange = 5
        cell.velocity = 300
        cell.velocityRange = 100
        cell.emissionLatitude = 0.9
        cell.alphaSpeed = -0.3
        cell.scale = 0.3
        cell.scaleRange = 0.1
        cell.scaleSpeed = -0.3
       
        cell.yAcceleration = 100
        cell.zAcceleration = 1000
        cell.emissionRange = CGFloat.pi * 2
       // 在  Assets.xcassets 导入一张名为star的图片
        cell.contents = UIImage(named:"star")!.cgImage
       // cell.color = CGColor.init(red: 1, green: 0.5, blue: 0.9, alpha: 1)
        
        let cell2 = CAEmitterCell()
        
        cell2.birthRate = 100
        cell2.lifetime = 10
        cell2.velocity = 800
        cell2.velocityRange = 100
        cell2.emissionLatitude = 0.9
        
        cell2.scale = 0.03
        cell2.scaleRange = 0.03
        cell2.scaleSpeed = -0.02
        cell2.lifetimeRange = 5
        cell2.yAcceleration = 100
        cell2.zAcceleration = 100
        cell2.emissionRange = CGFloat.pi * 2
       // 在  Assets.xcassets 导入一张名为balloon2的图片
        cell2.contents = UIImage(named:"balloon2")!.cgImage
        emitterLayer.emitterCells = [cell,cell2]
            
        view.layer.addSublayer(emitterLayer)
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        
    }
   
}

上一篇下一篇

猜你喜欢

热点阅读