先來(lái)看看效果圖:
下面直接上代碼:
粒子特效的話我只服蘋(píng)果系統(tǒng)的,CAEmitter
粒子特效提供了非常豐富的屬性來(lái)實(shí)現(xiàn)各種效果(雨滴、雪花、流星),用法簡(jiǎn)單B格高。首先創(chuàng)建好CAEmitterLayer
粒子發(fā)射器圖層,CAEmitterCell
粒子單元,然后根據(jù)需要設(shè)置somany
粒子單元的屬性就OK了,最后注意要將粒子發(fā)射器圖層的layer
添加到整個(gè)背景的sublayer
上。
@interface XMWeatherView ()@property(nonatomic,strong) CAEmitterLayer *sunshineEmitterLayer;@property(nonatomic,strong) CAEmitterLayer *rainDropEmitterLayer;@property(nonatomic,strong) UIImageView *backgroundView;@end
每個(gè)屬性都有詳細(xì)注釋,最后就發(fā)揮您的想象力,愛(ài)怎么玩怎么玩吧!
#pragma mark - 下雨特效-(void)addRainningEffect{ self.backgroundView.image=[UIImage imageNamed:@"rainning.jpeg"]; //粒子發(fā)射器圖層 self.rainDropEmitterLayer=[CAEmitterLayer layer]; //粒子發(fā)射器位置 _rainDropEmitterLayer.emitterPosition=CGPointMake(100, -30); //粒子發(fā)射器的范圍 _rainDropEmitterLayer.emitterSize=CGSizeMake(self.bounds.size.width*4, 0); //發(fā)射模式 _rainDropEmitterLayer.emitterMode=kCAEmitterLayerOutline; //粒子模式 _rainDropEmitterLayer.emitterShape=kCAEmitterLayerLine; //創(chuàng)建粒子 CAEmitterCell *emitterCell=[CAEmitterCell emitterCell]; //設(shè)置粒子內(nèi)容 emitterCell.contents=(__bridge id)([UIImage imageNamed:@"42-Raindrop"].CGImage); //設(shè)置粒子縮放比例 emitterCell.scale=0.9; //縮放范圍 emitterCell.scaleRange=0.5; //每秒粒子產(chǎn)生數(shù)量 emitterCell.birthRate=130; //粒子生命周期 emitterCell.lifetime=5; //粒子透明速度 emitterCell.alphaSpeed=-0.1; //粒子速度 emitterCell.velocity=280; emitterCell.velocityRange=100; //設(shè)置發(fā)射角度 emitterCell.emissionLongitude=-M_PI;// emitterCell.emissionRange=M_PI; //設(shè)置粒子旋轉(zhuǎn)角速度// emitterCell.spin=M_PI_4; //設(shè)置layer陰影 _rainDropEmitterLayer.shadowOpacity=1.0; //設(shè)置圓角 _rainDropEmitterLayer.shadowRadius=2; //設(shè)置偏移 _rainDropEmitterLayer.shadowOffset=CGSizeMake(1, 1); //設(shè)置顏色 _rainDropEmitterLayer.shadowColor=[UIColor whiteColor].CGColor ; //設(shè)置layer的粒子 _rainDropEmitterLayer.emitterCells=@[emitterCell]; _rainDropEmitterLayer.transform=CATransform3DMakeRotation(-M_PI/4, 0, 0, 1); [self.layer addSublayer:_rainDropEmitterLayer];}
櫻花的代碼大同小異,請(qǐng)自行腦補(bǔ)。
這一篇就到這里了,大家有什么意見(jiàn)和問(wèn)題記得及時(shí)反饋哦,希望本文對(duì)大家開(kāi)發(fā)iOS有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選