在學(xué)習(xí)動(dòng)畫的過程中,例子中有一個(gè)下雪的動(dòng)畫場景。用到的是粒子動(dòng)畫,覺得很神奇,特此記錄一下
snowView
import UIKit
import QuartzCore
class SnowView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
let emitter = layer as! CAEmitterLayer //粒子發(fā)射工廠
emitter.emitterPosition = CGPoint(x: bounds.size.width / 2, y: 0) //決定粒子發(fā)射形狀的中心點(diǎn)
emitter.emitterSize = bounds.size //決定粒子發(fā)射形狀的大小 決定了矩形的大小
emitter.emitterShape = kCAEmitterLayerRectangle //設(shè)置粒子從什么形狀發(fā)射出來 rectangle代表矩形。意思就是從矩形的嘴里面吐出粒子
let emitterCell = CAEmitterCell() //粒子
emitterCell.contents = UIImage(named: "flake.png")!.cgImage
emitterCell.birthRate = 200 //粒子產(chǎn)生的數(shù)量
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 //決定粒子的發(fā)射范圍
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
}
}
調(diào)用地方
//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框架
知行辦公,專業(yè)移動(dòng)辦公平臺(tái)https://zx.naton.cn/
【總監(jiān)】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【運(yùn)營】運(yùn)維艄公,897221533@qq.com;****
【產(chǎn)品設(shè)計(jì)】流浪貓,364994559@qq.com;
【體驗(yàn)設(shè)計(jì)】兜兜,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;
【數(shù)據(jù)】fox009521,42151960@qq.com;