DGThumbUpButton.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // DGThumbUpButton.m
  3. // DGThumbUpButton
  4. //
  5. // Created by Desgard_Duan on 16/6/9.
  6. // Copyright © 2016年 Desgard_Duan. All rights reserved.
  7. //
  8. #import "DGThumbUpButton.h"
  9. #import "DGExplodeAnimationView.h"
  10. @interface DGThumbUpButton()
  11. @property (strong, nonatomic) DGExplodeAnimationView *explodeAnimationView;
  12. @property (copy, nonatomic) void(^upInsideBlock)(BOOL selected);
  13. @end
  14. @implementation DGThumbUpButton
  15. - (void)awakeFromNib
  16. {
  17. [super awakeFromNib];
  18. [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
  19. // _isSelected = NO;
  20. // if (_isSelected) {
  21. // [self setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
  22. // } else {
  23. // [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
  24. // }
  25. [self addTarget: self
  26. action: @selector(clickButtonPress)
  27. forControlEvents: UIControlEventTouchUpInside];
  28. // self.backgroundColor = [UIColor redColor];
  29. [self insertSubview: self.explodeAnimationView atIndex: 0];
  30. }
  31. - (void)setIsSelected:(BOOL)isSelected {
  32. _isSelected = isSelected;
  33. if (isSelected) {
  34. [self setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
  35. } else {
  36. [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
  37. }
  38. }
  39. #pragma mark - Freedom Initial
  40. - (instancetype) initWithFrame: (CGRect)frame isPress: (BOOL)press type: (DGThumbUpButtonType)type {
  41. self = [super initWithFrame: frame];
  42. self.isSelected = press;
  43. if (self.isSelected) {
  44. [self setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
  45. } else {
  46. [self setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
  47. }
  48. [self addTarget: self
  49. action: @selector(clickButtonPress)
  50. forControlEvents: UIControlEventTouchUpInside];
  51. [self insertSubview: self.explodeAnimationView atIndex: 0];
  52. return self;
  53. }
  54. - (instancetype) initWithFrame: (CGRect)frame isPress: (BOOL)press {
  55. self = [self initWithFrame: frame
  56. isPress: press
  57. type: DGThumbUpExplosionType];
  58. return self;
  59. }
  60. #pragma mark - Override
  61. - (instancetype) initWithFrame: (CGRect) frame {
  62. self = [self initWithFrame: frame
  63. isPress: NO
  64. type: DGThumbUpExplosionType];
  65. return self;
  66. }
  67. - (instancetype) init {
  68. self = [self initWithFrame: CGRectMake(0, 0, 30, 30)
  69. isPress: NO
  70. type: DGThumbUpExplosionType];
  71. return self;
  72. }
  73. - (void) layoutSubviews {
  74. [super layoutSubviews];
  75. }
  76. #pragma mark - Methods
  77. - (void) clickButtonPress{
  78. if (self.isSelected) {
  79. [self popInsideWithDuration: 0.5];
  80. }
  81. else {
  82. [self popOutsideWithDuration: 0.5];
  83. if(self.isShowAnimation){
  84. [self.explodeAnimationView animate];
  85. }
  86. };
  87. }
  88. - (void) popOutsideWithDuration: (NSTimeInterval) duringTime {
  89. __weak typeof(self) weakSelf = self;
  90. self.transform = CGAffineTransformIdentity;
  91. [UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
  92. [weakSelf setImage: [UIImage imageNamed: @"pond_praise_yes"] forState: UIControlStateNormal];
  93. [UIView addKeyframeWithRelativeStartTime: 0
  94. relativeDuration: 1 / 3.0
  95. animations: ^{
  96. typeof(self) strongSelf = weakSelf;
  97. strongSelf.transform = CGAffineTransformMakeRotation(-30 * (M_PI / 180));
  98. }];
  99. [UIView addKeyframeWithRelativeStartTime: 0
  100. relativeDuration: 1 / 3.0
  101. animations: ^{
  102. typeof(self) strongSelf = weakSelf;
  103. strongSelf.transform = CGAffineTransformMakeScale(1.5, 1.5);
  104. }];
  105. [UIView addKeyframeWithRelativeStartTime: 1 / 3.0
  106. relativeDuration: 1 / 3.0
  107. animations: ^{
  108. typeof(self) strongSelf = weakSelf;
  109. strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
  110. }];
  111. [UIView addKeyframeWithRelativeStartTime: 2 / 3.0
  112. relativeDuration: 1 / 3.0
  113. animations: ^{
  114. typeof(self) strongSelf = weakSelf;
  115. strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
  116. }];
  117. } completion: ^(BOOL finished) {
  118. self.isSelected = YES;
  119. if(self.upInsideBlock){
  120. self.upInsideBlock(self.isSelected);
  121. }
  122. }];
  123. }
  124. - (void) popInsideWithDuration: (NSTimeInterval) duringTime {
  125. __weak typeof(self) weakSelf = self;
  126. self.transform = CGAffineTransformIdentity;
  127. [UIView animateKeyframesWithDuration: duringTime delay: 0 options: 0 animations: ^{
  128. [weakSelf setImage: [UIImage imageNamed: @"pond_praise_no"] forState: UIControlStateNormal];
  129. [UIView addKeyframeWithRelativeStartTime: 0
  130. relativeDuration: 1 / 2.0
  131. animations: ^{
  132. typeof(self) strongSelf = weakSelf;
  133. strongSelf.transform = CGAffineTransformMakeScale(0.8, 0.8);
  134. }];
  135. [UIView addKeyframeWithRelativeStartTime: 1 / 2.0
  136. relativeDuration: 1 / 2.0
  137. animations: ^{
  138. typeof(self) strongSelf = weakSelf;
  139. strongSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
  140. }];
  141. } completion: ^(BOOL finished) {
  142. self.isSelected = NO;
  143. if(self.upInsideBlock){
  144. self.upInsideBlock(self.isSelected);
  145. }
  146. }];
  147. }
  148. #pragma mark - Lazy Init
  149. - (DGExplodeAnimationView *) explodeAnimationView {
  150. if (!_explodeAnimationView) {
  151. _explodeAnimationView = [[DGExplodeAnimationView alloc] initWithFrame: self.bounds];
  152. }
  153. return _explodeAnimationView;
  154. }
  155. -(void)setActionUpInside:(void (^)(BOOL))action{
  156. [self setUpInsideBlock:action];
  157. }
  158. @end