如何添加像雪一样的雪。对UIView的影响

时间:2015-11-28 12:20:27

标签: ios uiview core-graphics core-animation

我目前正在申请圣诞节申请,我希望能够下雪#'在屏幕上。

到目前为止,我有这段代码:

Snow.swift

import Foundation
import UIKit

@IBDesignable
class Snow:UIView
{

    var viewHeight = CGFloat(0)
    /*
    required init(coder aDecoder: NSCoder) {
        //Initilse UIView
        super.init(coder: aDecoder)!
    }
    */

    override func drawRect(rect: CGRect) {
        let path = UIBezierPath(ovalInRect: rect)
        UIColor.whiteColor().setFill()
        path.fill()
    }
}

ViewController.swift

@IBDesignable
class ViewController: UIViewController {

    @IBInspectable var BgColor:UIColor = UIColor.whiteColor()

    var animator: UIDynamicAnimator? = nil;
    var gravity = UIGravityBehavior()


    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = BgColor
        /*listSubviewsOfView(self.view)*/ /*Not needed to answer this*/
        var snow = Snow(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
        snow.opaque = false
        self.view.addSubview(snow)

        animator = UIDynamicAnimator(referenceView:self.view);
        animator?.addBehavior(gravity)

        gravity.addItem(snow)
        let direction = CGVectorMake(0.0, 1.0)
        gravity.gravityDirection = direction
    }


    /*Not needed to solve this*/
    /*
    func listSubviewsOfView(views: UIView) {
        var index = 0
        let randomNumbers = [Int](1...24).shuffle()
        for view in views.subviews
        {
            if let _ = view.restorationIdentifier
            {
                view.setValue(String(Int(randomNumbers[index])), forKey: "updateText")
                index++
            }
            if index == randomNumbers.count {
                break
            }
        }
    }
    */

}

这就是它目前的样子(在iPhone 5模拟器中)

enter image description here

正如您所看到的,UIView落到屏幕底部的速度太快(像砖头而不是雪一样)。

我怎样才能得到“雪'随着风效应下降?

4 个答案:

答案 0 :(得分:2)

我会使用SKEmitterNode执行此操作。 SpriteKit中还有一个图形编辑器,您可以在其中轻松创建雪景效果。这是一个简短的教程:enter link description here

答案 1 :(得分:2)

使用UIKitDynamics的UIGravityBehaviour为对象制作动画。您可以使用UIKitDynamics

轻松创建出色的动画
    snowView = UIView(frame: CGRect(x: 100, y: 100, width: 20, height: 20))
    snowView.backgroundColor = UIColor.blueColor()
    view.addSubview(snowView)

    animator = UIDynamicAnimator(referenceView: view)
    gravity = UIGravityBehavior(items: [snowView])
    animator.addBehavior(gravity)

    animator.addBehavior(collision)

    itemBehaviour = UIDynamicItemBehavior(items: [snowView])
    itemBehaviour.elasticity = 0.7
    animator.addBehavior(itemBehaviour)

检查此链接以供参考: http://www.sitepoint.com/using-uikit-dynamics-swift-animate-apps/

答案 2 :(得分:1)

如果您目前没有使用SpriteKit,那么最好使用CAEmitterLayer参见

中的教程

http://www.enharmonichq.com/tutorial-particle-systems-in-core-animation-with-caemitterlayer/

这个想法是你在修道院视图的顶部添加一个与该视图大小相同的发射器层,顶部有一个发射雪花粒子的线形发射器单元。

粒子发射器包括对随机放置和随机运动的支持。 (雪花随着一些随机的左右运动向地面扑动。)

答案 3 :(得分:0)

我已经分享了所有的实现。但你需要flake.png你可以通过谷歌找到。

import UIKit
import QuartzCore

class ViewController: UIViewController {

  var snowView: SnowView!

  override func viewDidLoad() {
    super.viewDidLoad()

    //add the snow effect layer
    snowView = SnowView(frame: CGRect(x: -150, y:-100, width: 300, height: 50))
    let snowClipView = UIView(frame: CGRectOffset(view.frame, 0, 50))
    snowClipView.clipsToBounds = true
    snowClipView.addSubview(snowView)
    view.addSubview(snowClipView)

  }
}


import UIKit
import QuartzCore

class SnowView: UIView {

  override init(frame: CGRect) {
    super.init(frame: frame)

    let emitter = layer as! CAEmitterLayer
    emitter.emitterPosition = CGPoint(x: bounds.size.width / 2, y: 0)
    emitter.emitterSize = bounds.size
    emitter.emitterShape = kCAEmitterLayerRectangle

    let emitterCell = CAEmitterCell()
    emitterCell.contents = UIImage(named: "flake.png")!.CGImage
    emitterCell.birthRate = 200
    emitterCell.lifetime = 3.5
    emitterCell.color = UIColor.whiteColor().CGColor
    emitterCell.redRange = 0.0
    emitterCell.blueRange = 0.1
    emitterCell.greenRange = 0.0
    emitterCell.velocity = 10
    emitterCell.velocityRange = 350
    emitterCell.emissionRange = CGFloat(M_PI_2)
    emitterCell.emissionLongitude = CGFloat(-M_PI)
    emitterCell.yAcceleration = 70
    emitterCell.xAcceleration = 0
    emitterCell.scale = 0.33
    emitterCell.scaleRange = 1.25
    emitterCell.scaleSpeed = -0.25
    emitterCell.alphaRange = 0.5
    emitterCell.alphaSpeed = -0.15

    emitter.emitterCells = [emitterCell]
  }

  required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override class func layerClass() -> AnyClass {
    return CAEmitterLayer.self
  }

}