在学习动画的过程中,例子中有一个下雪的动画场景。用到的是粒子动画,觉得很神奇,特此记录一下
snowView
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 //设置粒子从什么形状发射出来 rectangle代表矩形。意思就是从矩形的嘴里面吐出粒子
let emitterCell = CAEmitterCell() //粒子
emitterCell.contents = UIImage(named: "flake.png")!.cgImage
emitterCell.birthRate = 200 //粒子产生的数量
emitterCell.lifetime = 3.5 //粒子的生命周期
emitterCell.color = UIColor.white.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 var layerClass: AnyClass {
return CAEmitterLayer.self
}
}
调用地方
//add the snow effect layer
snowView = SnowView(frame: CGRect(x: -150, y:-100, width: 300, height: 50))
let snowClipView = UIView(frame: view.frame.offsetBy(dx: 0, dy: 50))
snowClipView.clipsToBounds = true
snowClipView.addSubview(snowView)
view.addSubview(snowClipView)
如果想隐藏下雪功能
snowView.hidden = true
详情了解iOSQuartzCore框架
知行办公,专业移动办公平台https://zx.naton.cn/
【总监】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【运营】运维艄公,897221533@qq.com;****
【产品设计】流浪猫,364994559@qq.com;
【体验设计】兜兜,2435632247@qq.com;
【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;
【java】首席工程师MR_W,feixue300@qq.com;
【测试】土镜问道,847071279@qq.com;
【数据】fox009521,42151960@qq.com;