iOS - How to achieve a particle animation like effect on iOS

自古美人都是妖i 提交于 2019-12-24 00:53:09

问题


I have seen a particle scattering effect on a number of websites and app design concepts on dribbble. The effect is like this:- https://www.craftedbygc.com/

The effect can also be seen as shown in this link:- https://dribbble.com/shots/3511585-hello-dribbble

How can we achieve such an effect in an iOS application?


回答1:


If you're looking for something simple, check out CAEmitterLayer and CAEmitterCell. Just do a CAEmitterLayer with an emitterShape of kCAEmitterLayerRectangle and you can crop it with yet another CAShapeLayer to get the shape you want:


Here is a sample that generates something like the above:

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!
}

I don't know any easy way to produce something exactly like your examples, but you'll find third party libraries/demos out there (e.g. http://flexmonkey.blogspot.co.uk/2015/01/computing-particle-systems-in-swift.html).




回答2:


SpriteKit is a good choice.It has a feature called Particle Emitter. Lots of tutorial you will find helpful just like this to make such an effect



来源:https://stackoverflow.com/questions/44237975/ios-how-to-achieve-a-particle-animation-like-effect-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!