DGExplodeAnimationView.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // DGExplodeAnimationView.m
  3. // DGThumbUpButton
  4. //
  5. // Created by Desgard_Duan on 16/6/9.
  6. // Copyright © 2016年 Desgard_Duan. All rights reserved.
  7. //
  8. #import "DGExplodeAnimationView.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. @interface DGExplodeAnimationView()
  11. @property (nonatomic, strong) CAEmitterLayer *emitterLayer;
  12. @end
  13. @implementation DGExplodeAnimationView
  14. #pragma mark - Initial Function
  15. - (void) StartUp {
  16. self.clipsToBounds = NO;
  17. self.userInteractionEnabled = NO;
  18. CAEmitterCell *emitter = [CAEmitterCell emitterCell];
  19. emitter.contents = (id)[UIImage imageNamed: @"pond_praise_yes"].CGImage;
  20. emitter.name = @"explosion";
  21. emitter.alphaRange = 0.2f;
  22. emitter.alphaSpeed = -1.f;
  23. emitter.lifetime = 0.7f;
  24. emitter.lifetimeRange = 0.3f;
  25. emitter.birthRate = 0;
  26. emitter.velocity = 8.0f;
  27. emitter.velocityRange = 2.0f;
  28. emitter.emissionRange = M_PI_4;
  29. emitter.scale = 0.05f;
  30. emitter.scaleRange = 0.02;
  31. _emitterLayer = [CAEmitterLayer layer];
  32. _emitterLayer.name = @"emitterLayer";
  33. _emitterLayer.emitterShape = kCAEmitterLayerCircle;
  34. _emitterLayer.emitterMode = kCAEmitterLayerOutline;
  35. _emitterLayer.emitterPosition = CGPointMake(12, 12);//self.center;
  36. _emitterLayer.emitterSize = CGSizeMake(20, 0); //爆炸大小
  37. _emitterLayer.renderMode = kCAEmitterLayerOldestFirst;
  38. _emitterLayer.masksToBounds = NO;
  39. _emitterLayer.emitterCells = @[emitter];
  40. _emitterLayer.frame = CGRectMake(0, 0, 20, 20);//[UIScreen mainScreen].bounds; //爆炸大小
  41. [self.layer addSublayer: _emitterLayer];
  42. _emitterLayer.emitterPosition = CGPointMake(12, 12);//self.center;爆炸的x和y坐标
  43. }
  44. #pragma mark - Overide
  45. - (void) layoutSubviews {
  46. [super layoutSubviews];
  47. [self StartUp];
  48. }
  49. #pragma mark - Methods
  50. - (void) animate {
  51. dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC);
  52. dispatch_after(delay, dispatch_get_main_queue(), ^{
  53. self.emitterLayer.beginTime = CACurrentMediaTime();
  54. CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath: @"emitterCells.explosion.birthRate"];
  55. ani.fromValue = @0;
  56. ani.toValue = @200;
  57. [_emitterLayer addAnimation: ani forKey: nil];
  58. });
  59. }
  60. @end