YBAnchorPKView.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // YBAnchorPKView.m
  3. // yunbaolive
  4. //
  5. // Created by Boom on 2018/11/14.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "YBAnchorPKView.h"
  9. #import "YBPkProgressView.h"
  10. @implementation YBAnchorPKView{
  11. YBPkProgressView *proView;
  12. UIImageView *actionImage;
  13. NSTimer *timer;
  14. int timeCount;
  15. UILabel *timeL;
  16. UIImageView *resultImage;
  17. }
  18. - (instancetype)initWithFrame:(CGRect)frame andTime:(NSString *)time{
  19. self = [super initWithFrame:frame];
  20. timeCount = [time intValue];
  21. if (self) {
  22. [self creatUI];
  23. }
  24. return self;
  25. }
  26. - (void)creatUI{
  27. actionImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.height/2-10-_window_width*8/75, _window_width, _window_width*16/75)];
  28. actionImage.contentMode = UIViewContentModeScaleAspectFit;
  29. [self addSubview:actionImage];
  30. resultImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.height/2-10-_window_width*8/75, 56, 33)];
  31. resultImage.contentMode = UIViewContentModeScaleAspectFit;
  32. resultImage.hidden = YES;
  33. resultImage.center = actionImage.center;
  34. [self addSubview:resultImage];
  35. NSMutableArray *array = [NSMutableArray array];
  36. for (int i = 1; i<20; i++) {
  37. NSString *imageName=[NSString stringWithFormat:@"pk%d.png",i];
  38. NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
  39. UIImage *image=[UIImage imageWithContentsOfFile:path];
  40. [array addObject:image];
  41. }
  42. actionImage.animationImages=array;
  43. //一次动画的时间
  44. actionImage.animationDuration=[array count]*0.08;
  45. //只执行一次动画
  46. actionImage.animationRepeatCount = 1;
  47. //开始动画
  48. [actionImage startAnimating];
  49. //释放内存
  50. [actionImage performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:[array count]*0.08];
  51. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.52 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  52. actionImage.image = [UIImage imageNamed:@"pk19"];
  53. });
  54. proView = [[YBPkProgressView alloc]initWithFrame:CGRectMake(0, self.height-20, _window_width, 20)];
  55. [self addSubview:proView];
  56. timeL = [[UILabel alloc]initWithFrame:CGRectMake(_window_width/2-40, self.height-25-18, 80, 18)];
  57. timeL.font = [UIFont systemFontOfSize:10];
  58. timeL.text = [NSString stringWithFormat:@"PK%@ %@",YZMsg(@"时间"),[self seconds:timeCount]];
  59. timeL.textAlignment = NSTextAlignmentCenter;
  60. timeL.layer.cornerRadius = 9;
  61. timeL.layer.masksToBounds = YES;
  62. timeL.layer.borderWidth = 0.5;
  63. timeL.layer.borderColor = RGB_COLOR(@"#323232", 0.5).CGColor;
  64. timeL.backgroundColor = RGB_COLOR(@"#000000", 0.3);
  65. timeL.textColor = Pink_Cor;
  66. [self addSubview:timeL];
  67. if (!timer) {
  68. timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTimeL) userInfo:nil repeats:YES];
  69. }
  70. }
  71. - (void)changeTimeL{
  72. timeCount --;
  73. timeL.text = [NSString stringWithFormat:@"PK%@ %@",YZMsg(@"时间"),[self seconds:timeCount]];
  74. if (timeCount <= 0) {
  75. timeL.text = [NSString stringWithFormat:@"PK%@ 00:00",YZMsg(@"时间")];
  76. [timer invalidate];
  77. timer = nil;
  78. }
  79. }
  80. - (void)changeChengfaTimeL{
  81. timeCount --;
  82. timeL.text = [NSString stringWithFormat:@"%@ %@",YZMsg(@"惩罚时间"),[self seconds:timeCount]];
  83. if (timeCount <= 0) {
  84. timeL.text = [NSString stringWithFormat:@"%@ 00:00",YZMsg(@"惩罚时间")];
  85. [timer invalidate];
  86. timer = nil;
  87. if (self.pkViewEvent) {
  88. self.pkViewEvent();
  89. }
  90. }
  91. }
  92. - (void)showPkResult:(NSDictionary *)dic andWin:(int)win{
  93. if (timer) {
  94. [timer invalidate];
  95. timer = nil;
  96. }
  97. if (win == 0) {
  98. resultImage.image = [UIImage imageNamed:getImagename(@"平局icon")];
  99. resultImage.transform = CGAffineTransformMakeScale(0.1, 0.1);
  100. [UIView animateWithDuration:0.2 animations:^{
  101. actionImage.transform = CGAffineTransformMakeScale(0.1, 0.1);
  102. } completion:^(BOOL finished) {
  103. actionImage.hidden = YES;
  104. }];
  105. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  106. resultImage.hidden = NO;
  107. [UIView animateWithDuration:0.2 animations:^{
  108. resultImage.transform = CGAffineTransformMakeScale(1, 1);
  109. } completion:^(BOOL finished) {
  110. actionImage.hidden = YES;
  111. }];
  112. });
  113. YBWeakSelf;
  114. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  115. [UIView animateWithDuration:0.5 animations:^{
  116. resultImage.frame = CGRectMake(_window_width/2-28, self.height-20-33, 56, 33);
  117. } completion:^(BOOL finished) {
  118. if (weakSelf.pkViewEvent) {
  119. weakSelf.pkViewEvent();
  120. }
  121. }];
  122. });
  123. }else{
  124. timeCount = 60;
  125. timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeChengfaTimeL) userInfo:nil repeats:YES];
  126. timeL.text = [NSString stringWithFormat:@"%@ %@",YZMsg(@"惩罚时间"),[self seconds:timeCount]];
  127. resultImage.image = [UIImage imageNamed:getImagename(@"胜利icon")];
  128. resultImage.transform = CGAffineTransformMakeScale(0.1, 0.1);
  129. [UIView animateWithDuration:0.2 animations:^{
  130. actionImage.transform = CGAffineTransformMakeScale(0.1, 0.1);
  131. } completion:^(BOOL finished) {
  132. actionImage.hidden = YES;
  133. }];
  134. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  135. resultImage.hidden = NO;
  136. [UIView animateWithDuration:0.2 animations:^{
  137. resultImage.transform = CGAffineTransformMakeScale(1, 1);
  138. } completion:^(BOOL finished) {
  139. actionImage.hidden = YES;
  140. }];
  141. });
  142. if (win == 1) {
  143. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  144. [UIView animateWithDuration:0.5 animations:^{
  145. resultImage.frame = CGRectMake(_window_width/4-28, self.height-20-33, 56, 33);
  146. } completion:^(BOOL finished) {
  147. }];
  148. });
  149. }else{
  150. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  151. [UIView animateWithDuration:0.5 animations:^{
  152. resultImage.frame = CGRectMake(_window_width*3/4-28, self.height-20-33, 56, 33);
  153. } completion:^(BOOL finished) {
  154. }];
  155. });
  156. }
  157. }
  158. }
  159. - (void)updateProgress:(CGFloat)progress withBlueNum:(NSString *)blueNum withRedNum:(NSString *)redNum{
  160. [proView updateProgress:progress withBlueNum:blueNum withRedNum:redNum];
  161. }
  162. - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  163. UIView *hitView = [super hitTest:point withEvent:event];
  164. if(hitView == self){
  165. return nil;
  166. }
  167. return hitView;
  168. }
  169. - (NSString *)seconds:(int)s{
  170. NSString *str;
  171. str = [NSString stringWithFormat:@"%02d:%02d",s/60,s%60];
  172. return str;
  173. }
  174. - (void)removeTimer{
  175. if (timer) {
  176. [timer invalidate];
  177. timer = nil;
  178. }
  179. }
  180. @end