CAEmitterLayer屬性
//裝著CAEmitterCell對象的數(shù)組啊犬,被用于把粒子投放到layer上
@property(nullable, copy) NSArray<CAEmitterCell *> *emitterCells;
//粒子產(chǎn)生系數(shù)拙已,默認1.0
@property float birthRate;
//粒子生命周期
@property float lifetime;
//發(fā)射位置
@property CGPoint emitterPosition;
//發(fā)射源的z坐標(biāo)位置
@property CGFloat emitterZPosition;
//發(fā)射源的大小
@property CGSize emitterSize;
//決定粒子形狀的深度聯(lián)系:emitter shape
@property CGFloat emitterDepth;
//發(fā)射源的形狀
@property(copy) NSString *emitterShape;
- 取值
NSString * const kCAEmitterLayerPoint;
NSString * const kCAEmitterLayerLine;
NSString * const kCAEmitterLayerRectangle;
NSString * const kCAEmitterLayerCuboid;
NSString * const kCAEmitterLayerCircle;
NSString * const kCAEmitterLayerSphere;
//發(fā)射模式
@property(copy) NSString *emitterMode;
- 取值
NSString * const kCAEmitterLayerPoints;
NSString * const kCAEmitterLayerOutline;
NSString * const kCAEmitterLayerSurface;
NSString * const kCAEmitterLayerVolume;
//渲染模式
@property(copy) NSString *renderMode;
- 取值
NSString * const kCAEmitterLayerUnordered;
NSString * const kCAEmitterLayerOldestFirst;
NSString * const kCAEmitterLayerOldestLast;
NSString * const kCAEmitterLayerBackToFront;
NSString * const kCAEmitterLayerAdditive;
//不是多很清楚(粒子是平展在層上)
@property BOOL preservesDepth;
//粒子速度
@property float velocity;
//粒子的縮放比例
@property float scale;
//自旋轉(zhuǎn)速度
@property float spin;
//用于初始化隨機數(shù)產(chǎn)生的種子
@property unsigned int seed;
CAEmitterCell屬性
//初始化方法
+ (instancetype)emitterCell;
//根據(jù)健 獲 得 值
+ (nullable id)defaultValueForKey:(NSString *)key;
//是否 歸 檔莫 鍵值
- (BOOL)shouldArchiveValueForKey:(NSString *)key;
//粒子的名字
@property(nullable, copy) NSString *name;
//粒子是否被渲染
@property(getter=isEnabled) BOOL enabled;
//粒子參數(shù)的速度乘數(shù)因子
@property float birthRate;
//生命周期
@property float lifetime;
//生命周期范圍
@property float lifetimeRange;
//發(fā)射的 z 軸方向的角度
@property CGFloat emissionLatitude;
//x-y 平面的 發(fā) 射方向
@property CGFloat emissionLongitude;
//周 圍發(fā)射角度
@property CGFloat emissionRange;
//速度
@property CGFloat velocity;
//速度范圍
@property CGFloat velocityRange;
//粒子 x 方向的加速度分量
@property CGFloat xAcceleration;
//粒子 y 方向的加速度分量
@property CGFloat yAcceleration;
//粒子 z 方向的加速度分量
@property CGFloat zAcceleration;
//縮放比例
@property CGFloat scale;
//縮放比例范圍
@property CGFloat scaleRange;
//縮放比例速度
@property CGFloat scaleSpeed;
//子旋轉(zhuǎn)角度
@property CGFloat spin;
//子旋轉(zhuǎn)角度范圍
@property CGFloat spinRange;
//粒子的顏色
@property(nullable) CGColorRef color;
//一個粒子的 顏 色 red 能改 變 的范 圍
@property float redRange;
//一個粒子的 顏 色 green 能改 變 的范 圍
@property float greenRange;
//一個粒子的 顏 色 blue 能改 變 的范 圍
@property float blueRange;
//一個粒子的 顏 色 alpha 能改 變 的范 圍
@property float alphaRange;
//粒子 red 在生命周期內(nèi)的改變速度
@property float redSpeed;
//粒子 green 在生命周期內(nèi)的改變速度
@property float greenSpeed;
//粒子 blue 在生命周期內(nèi)的改變速度
@property float blueSpeed;
//粒子透明度在生命周期內(nèi)的改變速度
@property float alphaSpeed;
//是個 CGImageRef 的對象 , 既粒子要展現(xiàn)的圖片
@property(nullable, strong) id contents;
//應(yīng)該畫在 contents 里的子 rectangle
@property CGRect contentsRect;
//定義了寄宿圖的像素尺寸和視圖大小的比例冒掌,默認情況下它是一個值為1.0的浮點數(shù)
@property CGFloat contentsScale;
//減小自己的大小
@property(copy) NSString *minificationFilter;
//不是很清楚好像增加自己的大小
@property(copy) NSString *magnificationFilter;
//減小大小的因子
@property float minificationFilterBias;
//粒子發(fā)射的粒子
@property(nullable, copy) NSArray<CAEmitterCell *> *emitterCells;
//類似于層的繼承的屬性(不是很清楚)
@property(nullable, copy) NSDictionary *style;
例子
粒子效果.gif
.h文件
@interface EmitterButton : UIControl
/**是否選中***/
@property (assign,nonatomic,readonly) BOOL chose;
/**選中回調(diào)***/
@property (nonatomic, copy) void (^choseClick)(EmitterButton *button);
-(instancetype)initWithFrame:(CGRect)frame andNormalImage:(UIImage *)image andHighlightImage:(UIImage *)highlightImage andEffectImage:(UIImage *)EffImage;
@end
.m文件
#import "EmitterButton.h"
@interface EmitterButton ()
{
UIImage *Nimage;
UIImage *Himage;
UIImage *Eimage;
UIImageView *imageView;
BOOL isChose;
CAEmitterLayer *emitterLayer;
CAEmitterCell *emitterCell;
}
@end
@implementation EmitterButton
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
Himage = [UIImage imageNamed:@"Zan"];
Nimage = [UIImage imageNamed:@"UnZan"];
Eimage = [UIImage imageNamed:@"EffectImage"];
[self setUpSubviews];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame andNormalImage:(UIImage *)image andHighlightImage:(UIImage *)highlightImage andEffectImage:(UIImage *)EffImage
{
self = [super initWithFrame:frame];
if (self) {
Nimage = image;
Himage = highlightImage;
Eimage = EffImage;
[self setUpSubviews];
}
return self;
}
-(void)setUpSubviews
{
imageView = [[UIImageView alloc]init];
imageView.frame = self.bounds;
imageView.userInteractionEnabled = YES;
[imageView setImage:Nimage];
[self addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTap)];
[imageView addGestureRecognizer:tap];
emitterLayer = [CAEmitterLayer layer];
//設(shè)置發(fā)射位置
[emitterLayer setEmitterPosition:CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0)];
//設(shè)置發(fā)射源的大小
[emitterLayer setEmitterSize:CGSizeMake(CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
//設(shè)置發(fā)射源的形狀
[emitterLayer setEmitterShape:kCAEmitterLayerCircle];
//設(shè)置發(fā)射模式
[emitterLayer setEmitterMode:kCAEmitterLayerOutline];
[self.layer addSublayer:emitterLayer];
emitterCell = [CAEmitterCell emitterCell];
//設(shè)置粒子的名字
[emitterCell setName:@"emitterButton"];
//設(shè)置粒子速度
[emitterCell setVelocity:50];
//設(shè)置粒子速度范圍
[emitterCell setVelocityRange:50];
//設(shè)置粒子參數(shù)的速度乘數(shù)因子
[emitterCell setBirthRate:0];
//設(shè)置粒子生命周期
[emitterCell setLifetime:1.0];
//設(shè)置粒子透明度在生命周期內(nèi)的改變速度
[emitterCell setAlphaSpeed:-1];
//設(shè)置粒子要展現(xiàn)的圖片,是個 CGImageRef 的對象
[emitterCell setContents:(__bridge id)Eimage.CGImage];
[emitterLayer setEmitterCells:@[emitterCell]];
}
-(void)imageViewTap{
isChose = !isChose;
[self setCurrentImage];
imageView.bounds = CGRectZero;
[UIView animateWithDuration:0.25 delay:0 options:0.3 animations:^{
[imageView setBounds:self.bounds];
if (isChose)
{
CABasicAnimation *baseAnimat = [CABasicAnimation animationWithKeyPath:@"emitterCells.emitterButton.birthRate"];
[baseAnimat setFromValue:[NSNumber numberWithFloat:100]];
[baseAnimat setToValue:[NSNumber numberWithFloat:0]];
baseAnimat.duration = 0;
baseAnimat.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[emitterLayer addAnimation:baseAnimat forKey:@"effectButton"];
}
} completion:nil];
}
-(void)setCurrentImage
{
if (isChose) {
[imageView setImage:Himage];
}else{
[imageView setImage:Nimage];
}
}
-(BOOL)chose
{
return isChose;
}
@end