粒子動畫CAEmitterLayer CAEmitterCell

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

文章來自http://blog.csdn.net/hufengvip/article/details/7617042

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末跪者,一起剝皮案震驚了整個濱河市疲迂,隨后出現(xiàn)的幾起案子挟裂,更是在濱河造成了極大的恐慌,老刑警劉巖遣鼓,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異骨田,居然都是意外死亡耿导,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門态贤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來舱呻,“玉大人,你說我怎么就攤上這事悠汽∠渎溃” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵柿冲,是天一觀的道長茬高。 經(jīng)常有香客問我,道長姻采,這世上最難降的妖魔是什么雅采? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮慨亲,結(jié)果婚禮上婚瓜,老公的妹妹穿的比我還像新娘。我一直安慰自己刑棵,他們只是感情好巴刻,可當(dāng)我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蛉签,像睡著了一般胡陪。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碍舍,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天柠座,我揣著相機與錄音,去河邊找鬼片橡。 笑死妈经,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捧书。 我是一名探鬼主播吹泡,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼经瓷!你這毒婦竟也來了爆哑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤舆吮,失蹤者是張志新(化名)和其女友劉穎揭朝,沒想到半個月后队贱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡萝勤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年露筒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片敌卓。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡慎式,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出趟径,到底是詐尸還是另有隱情瘪吏,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布蜗巧,位于F島的核電站掌眠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏幕屹。R本人自食惡果不足惜蓝丙,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望望拖。 院中可真熱鬧渺尘,春花似錦、人聲如沸说敏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盔沫。三九已至医咨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間架诞,已是汗流浹背拟淮。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留谴忧,地道東北人很泊。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像俏蛮,于是被迫代替她去往敵國和親撑蚌。 傳聞我的和親對象是個殘疾皇子上遥,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,152評論 25 707
  • 在此特此聲明:一下所有鏈接均來自互聯(lián)網(wǎng)搏屑,在此記錄下我的查閱學(xué)習(xí)歷程,感謝各位原創(chuàng)作者的無私奉獻 粉楚! 技術(shù)一點一點積...
    遠航的移動開發(fā)歷程閱讀 11,123評論 12 197
  • 網(wǎng)站: http://ui4app.com/category 技術(shù)網(wǎng)站: http://www.bestsdk.c...
    LYPC_下里巴人閱讀 1,014評論 0 1
  • 對于 iOS 框架的一些簡單介紹 (顏色表示常用程度) Cocoa Touch GameKit 實現(xiàn)對游戲中心的支...
    飛奔的羊閱讀 885評論 0 3
  • 【作家簡介】 筆名簡·薩默斯辣恋,英國女作家亮垫,被尊為女權(quán)偶像人物的文學(xué)老祖母,與伍爾芙并稱“雙星”伟骨,幾次獲得諾貝...
    2a0e277a3c58閱讀 206評論 0 0