【问题标题】:iOS - How to achieve a particle animation like effect on iOSiOS - 如何在 iOS 上实现类似粒子动画的效果
【发布时间】:2017-05-29 08:33:41
【问题描述】:

我在 dribbble 上看到了许多网站和应用程序设计概念上的粒子散射效果。效果是这样的:-https://www.craftedbygc.com/

效果也可以看这个链接:- https://dribbble.com/shots/3511585-hello-dribbble

我们如何在 iOS 应用程序中实现这样的效果?

【问题讨论】:

    标签: ios swift canvas particles


    【解决方案1】:

    如果您正在寻找简单的东西,请查看CAEmitterLayerCAEmitterCell。只需使用emitterShapekCAEmitterLayerRectangle 进行CAEmitterLayer,您可以使用另一个CAShapeLayer 对其进行裁剪,以获得您想要的形状:


    这是一个生成类似上述内容的示例:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        // define emitter layer as centered w 80% of smallest dimension
    
        let image = emitterImage
        let side = min(view.bounds.width, view.bounds.height) * 0.8
        let origin = CGPoint(x: view.bounds.midX - side / 2, y: view.bounds.midY - side / 2)
        let center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
        let size = CGSize(width: side, height: side)
        let rect = CGRect(origin: origin, size: size)
        let emitterLayer = CAEmitterLayer()
        emitterLayer.emitterShape = kCAEmitterLayerRectangle
        emitterLayer.emitterSize = rect.size
        emitterLayer.emitterPosition = center
    
        // define cells
    
        let cell = CAEmitterCell()
        cell.birthRate = Float(size.width * size.height / 10)
        cell.lifetime = 1
        cell.velocity = 10
        cell.scale = 0.1
        cell.scaleSpeed = -0.1
        cell.emissionRange = .pi * 2
        cell.contents = image.cgImage
        emitterLayer.emitterCells = [cell]
    
        // add the layer
    
        view.layer.addSublayer(emitterLayer)
    
        // mask the layer
    
        let lineWidth = side / 8
        let mask = CAShapeLayer()
        mask.fillColor = UIColor.clear.cgColor
        mask.strokeColor = UIColor.white.cgColor
        mask.lineWidth = lineWidth
        mask.path = UIBezierPath(arcCenter: center, radius: (side - lineWidth) / 2, startAngle: .pi / 4, endAngle: .pi * 7 / 4, clockwise: true).cgPath
        emitterLayer.mask = mask
    }
    
    var emitterImage: UIImage {
        let rect = CGRect(origin: .zero, size: CGSize(width: 10, height: 10))
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
        #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1).setFill()
        UIBezierPath(arcCenter: CGPoint(x: rect.midX, y: rect.midY), radius: rect.midX, startAngle: 0, endAngle: .pi * 2, clockwise: true).fill()
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    

    我不知道有什么简单的方法可以生成与您的示例完全相同的东西,但是您会在那里找到第三方库/演示(例如http://flexmonkey.blogspot.co.uk/2015/01/computing-particle-systems-in-swift.html)。

    【讨论】:

    • 先生,您能否为我提供一些带有发射器层的代码,用于这种特殊的示例?有没有通过swift代码实现gif中的效果?
    【解决方案2】:

    SpriteKit 是一个不错的选择。它有一个名为Particle Emitter的功能。很多教程你会发现只有like this 才能产生这样的效果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 2012-10-10
      相关资源
      最近更新 更多