iOS核心动画学习整理
最近利用業(yè)余時(shí)間終于把iOS核心動(dòng)畫(huà)高級(jí)技巧(https://zsisme.gitbooks.io/ios-/content/chapter1/the-layer-tree.html)看完,對(duì)應(yīng)其中一些知識(shí)做了相應(yīng)的整理,整理為demo(https://github.com/PurpleSweetPotatoes/Layer_learn)。此demo中都是基于教程書(shū)籍中的編程示例,并加上了注解以方便各位iOS愛(ài)好者學(xué)習(xí)使用。
在這里利用此教程中的基礎(chǔ)知識(shí)做了2個(gè)小demo,活動(dòng)指示器效果和火焰效果。先讓我們看看效果圖
??
在這里我需要使用到CALayer的兩個(gè)子類(lèi)CAReplicatorLayer和CAEmitterLayer。
1.CAReplicatorLayer的目的是為了高效生成許多相似的圖層。它會(huì)繪制一個(gè)或多個(gè)圖層的子圖層,并在每個(gè)復(fù)制體上應(yīng)用不同的變換。此處直接搬出運(yùn)行代碼
1 //需要實(shí)例化的個(gè)數(shù) 2 int numofInstance = 10; 3 //動(dòng)畫(huà)時(shí)長(zhǎng) 4 CGFloat duration = 1.0f; 5 //創(chuàng)建repelicator對(duì)象 6 CAReplicatorLayer *repelicator = [CAReplicatorLayer layer]; 7 //設(shè)置其位置 8 repelicator.frame = self.view.bounds; 9 //需要生成多少個(gè)相同實(shí)例 10 repelicator.instanceCount = numofInstance; 11 //代表實(shí)例生成的延時(shí)時(shí)間; 12 repelicator.instanceDelay = duration / numofInstance; 13 //設(shè)置每個(gè)實(shí)例的變換樣式 14 repelicator.instanceTransform = CATransform3DMakeRotation(M_PI * 2.0 / 10.0, 0, 0, 1); 15 16 //創(chuàng)建repelicator對(duì)象的子圖層,repelicator會(huì)利用此子圖層進(jìn)行高效復(fù)制。并繪制到自身圖層上 17 CALayer *layer = [CALayer layer]; 18 //設(shè)置子圖層的大小位置 19 layer.frame = CGRectMake(0, 0, 10, 10); 20 //子圖層的仿射變換是基于repelicator圖層的錨點(diǎn),因此這里將子圖層的位置擺放到此錨點(diǎn)附近。 21 CGPoint point = [repelicator convertPoint:repelicator.position fromLayer:self.view.layer]; 22 layer.position = CGPointMake(point.x, point.y - 20); 23 //設(shè)置子圖層的背景色 24 layer.backgroundColor = [UIColor whiteColor].CGColor; 25 //將子圖層切圓 26 layer.cornerRadius = 5; 27 //將子圖層添加到repelicator上 28 [repelicator addSublayer:layer]; 29 //對(duì)layer進(jìn)行動(dòng)畫(huà)設(shè)置 30 CABasicAnimation *animaiton = [CABasicAnimation animation]; 31 //設(shè)置動(dòng)畫(huà)所關(guān)聯(lián)的路徑屬性 32 animaiton.keyPath = @"transform.scale"; 33 //設(shè)置動(dòng)畫(huà)起始和終結(jié)的動(dòng)畫(huà)值 34 animaiton.fromValue = @(1); 35 animaiton.toValue = @(0.1); 36 //設(shè)置動(dòng)畫(huà)時(shí)間 37 animaiton.duration = duration; 38 //設(shè)置動(dòng)畫(huà)次數(shù) 39 animaiton.repeatCount = INT_MAX; 40 //添加動(dòng)畫(huà) 41 [layer addAnimation:animaiton forKey:nil];2.CAEmitterLayer是一個(gè)高性能的粒子引擎,被用來(lái)創(chuàng)建實(shí)時(shí)例子動(dòng)畫(huà)如:煙霧,火,雨等等這些效果。CAEmitterLayer看上去像是許多CAEmitterCell的容器,這些CAEmitterCell定義了一個(gè)粒子效果。下面直接上代碼
1 - (CAEmitterLayer *)emitter { 2 if (_emitter == nil) { 3 //創(chuàng)建粒子圖層容器 4 CAEmitterLayer *layer = [CAEmitterLayer layer]; 5 //設(shè)置發(fā)射器位置 6 layer.emitterPosition = self.view.center; 7 //設(shè)置發(fā)射器范圍 8 layer.emitterSize = CGSizeMake(50, 30); 9 //設(shè)置發(fā)射器形狀 10 layer.emitterShape = kCAEmitterLayerLine; 11 //設(shè)置發(fā)射器發(fā)射模式 12 layer.emitterMode = kCAEmitterLayerSurface; 13 //設(shè)置發(fā)射器的渲染模式 14 layer.renderMode = kCAEmitterLayerAdditive; 15 16 _emitter = layer; 17 } 18 return _emitter; 19 }接著創(chuàng)建CAEmitterCell粒子圖層并放入CAEmitterLayer容器中
1 //創(chuàng)建粒子 2 CAEmitterCell *cell = [CAEmitterCell emitterCell]; 3 //配置粒子圖像 4 cell.contents = (__bridge id)[UIImage imageNamed:@"火焰"].CGImage; 5 //粒子每秒發(fā)射個(gè)數(shù) 6 cell.birthRate = 400; 7 //粒子生命周期 8 cell.lifetime = 3.0; 9 //粒子旋轉(zhuǎn)角度 10 cell.spin = M_PI / 2; 11 //粒子顏色 12 cell.color = [UIColor colorWithRed:1.000 green:0.681 blue:0.178 alpha:1.000].CGColor; 13 //粒子衰減過(guò)程 14 cell.alphaSpeed -= 1; 15 //粒子發(fā)射速度 16 cell.velocity = 40; 17 //粒子發(fā)射速度可變范圍,如果此處設(shè)置為10 則速度值在30 - 50 之類(lèi)變動(dòng) 18 cell.velocityRange = 10; 19 //粒子縮放比例 20 cell.scaleSpeed -= 1; 21 //cell發(fā)射角度范圍 22 cell.emissionLongitude = 0; 23 //cell發(fā)射偏轉(zhuǎn)角度 24 cell.emissionRange = M_PI / 4; 25 //將配置好的粒子放入容器中 26 self.emitter.emitterCells = @[cell];此處編寫(xiě)這兩種效果圖旨在引起各位iOS愛(ài)好者的對(duì)動(dòng)畫(huà)和layer層學(xué)習(xí)的興趣,如想深入學(xué)習(xí)請(qǐng)點(diǎn)擊上面鏈接自行學(xué)習(xí)。另外歡迎大家與筆者一起學(xué)習(xí)交流,對(duì)于只求代碼者一律不回,謝謝!上述有任何錯(cuò)誤歡迎指正!
轉(zhuǎn)載于:https://www.cnblogs.com/purple-sweet-pottoes/p/5003295.html
總結(jié)
以上是生活随笔為你收集整理的iOS核心动画学习整理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 视频弹幕开源库
- 下一篇: 守望者的逃离—贪心算法