Skip to content

Instantly share code, notes, and snippets.

@minsOne
Last active January 30, 2020 21:16
Show Gist options
  • Save minsOne/71e7d761489e70e5f63a to your computer and use it in GitHub Desktop.
Save minsOne/71e7d761489e70e5f63a to your computer and use it in GitHub Desktop.
Falling Snow
//
// SnowView.swift
// fallingsnow
//
// Created by JungMin Ahn on 2015. 11. 19..
// Copyright © 2015년 SmartStudy. All rights reserved.
//
import UIKit
import SpriteKit
class SnowScene: SKScene {
private var presentingView: SKView?
private var emitter: SKEmitterNode?
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
scaleMode = .ResizeFill
backgroundColor = UIColor.clearColor()
presentingView = view
}
func startEmitter() {
emitter = SKEmitterNode(fileNamed: "snow.sks")
guard
let emitter = emitter,
let presentingView = presentingView
else { return }
emitter.particlePositionRange = CGVectorMake(CGRectGetWidth(presentingView.bounds), 0)
emitter.position = CGPointMake(CGRectGetMidX(presentingView.bounds), CGRectGetHeight(presentingView.bounds))
emitter.targetNode = self
addChild(emitter)
}
func stopEmitter() {
guard let emitter = emitter else { return }
emitter.particleBirthRate = 0.0
emitter.targetNode = nil
emitter.removeFromParent()
self.emitter = nil
}
}
//
// ViewController.swift
// fallingsnow
//
// Created by JungMin Ahn on 2015. 11. 19..
// Copyright © 2015년 SmartStudy. All rights reserved.
//
import UIKit
import SpriteKit
class ViewController: UIViewController {
@IBOutlet weak private var snowView: UIView!
private var sceneView: SKView?
private var snowScene: SnowScene?
override func viewDidLoad() {
super.viewDidLoad()
sceneView = SKView(frame: self.view.frame)
snowScene = SnowScene()
guard
let sceneView = sceneView,
let snowScene = snowScene
else { return }
sceneView.backgroundColor = UIColor.clearColor()
sceneView.presentScene(snowScene)
snowView.addSubview(sceneView)
sceneView.userInteractionEnabled = false
snowView.userInteractionEnabled = false
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
guard let snowScene = snowScene else { return }
snowScene.startEmitter()
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
guard
let snowScene = snowScene,
let sceneView = sceneView
else { return }
sceneView.presentScene(nil)
sceneView.removeFromSuperview()
snowScene.stopEmitter()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment