如何向 UIView 添加 'snow like' 效果
How do I add a 'snow like' effect to a UIView
我目前正在制作圣诞节应用程序,我希望它 'snow' 显示在屏幕上。
到目前为止我有这个代码:
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 模拟器中)
如您所见,UIView
掉到屏幕底部的速度太快了(像砖头一样掉落而不是像雪一样掉落)。
如何让 'snow' 随着风的影响下降得更慢?
我会用 SKEmitterNode 来做这个。 SpriteKit 中还有一个图形编辑器,您可以在其中轻松创建雪花效果。这是关于此的简短教程:enter link description here
如果您目前没有使用 SpriteKit,那么最好使用 CAEmitterLayer 请参阅
上的教程
http://www.enharmonichq.com/tutorial-particle-systems-in-core-animation-with-caemitterlayer/
我们的想法是,您将在修道院视图的顶部添加一个与该视图大小相同的发射器层,顶部有一个线形发射器单元,用于发射雪花粒子。
粒子发射器包括对随机放置和随机运动的支持。 (雪花以随机的左右运动飘落到地面。)
使用 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)
检查此 Link 以供参考:
http://www.sitepoint.com/using-uikit-dynamics-swift-animate-apps/
我已经分享了所有的实现。但是你需要 flake.png 你可以通过 google 找到。
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
}
}
我目前正在制作圣诞节应用程序,我希望它 'snow' 显示在屏幕上。
到目前为止我有这个代码:
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 模拟器中)
如您所见,UIView
掉到屏幕底部的速度太快了(像砖头一样掉落而不是像雪一样掉落)。
如何让 'snow' 随着风的影响下降得更慢?
我会用 SKEmitterNode 来做这个。 SpriteKit 中还有一个图形编辑器,您可以在其中轻松创建雪花效果。这是关于此的简短教程:enter link description here
如果您目前没有使用 SpriteKit,那么最好使用 CAEmitterLayer 请参阅
上的教程http://www.enharmonichq.com/tutorial-particle-systems-in-core-animation-with-caemitterlayer/
我们的想法是,您将在修道院视图的顶部添加一个与该视图大小相同的发射器层,顶部有一个线形发射器单元,用于发射雪花粒子。
粒子发射器包括对随机放置和随机运动的支持。 (雪花以随机的左右运动飘落到地面。)
使用 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)
检查此 Link 以供参考: http://www.sitepoint.com/using-uikit-dynamics-swift-animate-apps/
我已经分享了所有的实现。但是你需要 flake.png 你可以通过 google 找到。
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
}
}