I'm currently making a Christmas application, and I would like it to be "snow" on the screen.
So far I have this code:
Snow.swift
import Foundation
import UIKit
@IBDesignable
class Snow:UIView
{
var viewHeight = CGFloat(0)
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
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
}
}
This is what it currently looks like (in the iPhone 5 simulator)

As you can see, UIViewit falls too quickly to the bottom of the screen (it falls like a brick instead of snow).
How could I “snow” fall more slowly with the wind effect?
source
share