FPKProgress.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // FPKProgress.m
  3. // FanweApp
  4. //
  5. // Created by 志刚杨 on 2018/7/17.
  6. // Copyright © 2018年 xfg. All rights reserved.
  7. //
  8. #import "FPKProgress.h"
  9. #define KPKRightOrLeftHeiht 20
  10. #define KPKAudienceHeiht 36
  11. @implementation FPKProgress
  12. {
  13. UIView *pkRight;
  14. UIView *pkLeft;
  15. UILabel *valueLeft;
  16. UILabel *valueRight;
  17. // UILabel *labTimer;//定时器
  18. // NSTimer *timer;
  19. UIImageView *pkImage;
  20. // UIImageView *pkDdate;
  21. // UIButton *avatarImage;
  22. //2020-1-3 修改pk进度
  23. // UIImageView *pkImageView;
  24. UIImageView *leftResultView;
  25. UIImageView *rightResultView;
  26. }
  27. - (instancetype)initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. // self.backgroundColor = kGreenColor;
  32. [self createUI];
  33. }
  34. return self;
  35. }
  36. - (void)handleAvaTarEvent:(UIButton *)button {
  37. }
  38. - (void)setAvatar:(NSString *)avatar
  39. {
  40. _avatar = avatar;
  41. // [avatarImage sd_setImageWithURL:[NSURL URLWithString:self.avatar] forState:UIControlStateNormal placeholderImage:kDefaultPreloadHeadImg];
  42. }
  43. -(void)createUI
  44. {
  45. // avatarImage = [[UIButton alloc] init];
  46. // [avatarImage sd_setImageWithURL:[NSURL URLWithString:self.avatar] forState:UIControlStateNormal placeholderImage:kDefaultPreloadHeadImg];
  47. // [self addSubview:avatarImage];
  48. // avatarImage.frame = CGRectMake(self.width - 80, 2, 30, 30);
  49. // // avatarImage.backgroundColor = kRedColor;
  50. // avatarImage.layer.cornerRadius = avatarImage.width/2;
  51. // avatarImage.layer.masksToBounds = YES;
  52. // [avatarImage addTarget:self action:@selector(handleAvaTarEvent:) forControlEvents:UIControlEventTouchUpInside];
  53. pkLeft = [[UIView alloc] init];
  54. pkLeft.backgroundColor =UIColorFromRGB(0x23B6F0);
  55. pkRight = [[UIView alloc] init];
  56. pkRight.backgroundColor =UIColorFromRGB(0xF5514D);
  57. pkLeft.backgroundColor = [UIColor colorWithHexString:@"#4FB5FA"];
  58. pkRight.backgroundColor = [UIColor colorWithHexString:@"#ED3B88"];
  59. pkLeft.frame = CGRectMake(0, 0, kScreenW / 2, KPKRightOrLeftHeiht);
  60. pkRight.frame = CGRectMake(0, 0, kScreenW, KPKRightOrLeftHeiht);
  61. self.pkAudienceView.frame = CGRectMake(0, KPKRightOrLeftHeiht, kScreenW, kRealValue(KPKAudienceHeiht));
  62. self.pkAudienceView.backgroundColor = kClearColor;
  63. [self addSubview:pkRight];
  64. [self addSubview:pkLeft];
  65. [self addSubview:self.pkAudienceView];
  66. //TODO:uiview 单边圆角或者单边框
  67. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:pkRight.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(0,0)];//圆角大小
  68. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  69. maskLayer.frame = pkRight.bounds;
  70. maskLayer.path = maskPath.CGPath;
  71. pkRight.layer.mask = maskLayer;
  72. //TODO:uiview 单边圆角或者单边框
  73. UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:pkLeft.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(0,0)];//圆角大小
  74. CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
  75. maskLayer1.frame = pkLeft.bounds;
  76. maskLayer1.path = maskPath1.CGPath;
  77. pkLeft.layer.mask = maskLayer1;
  78. // pkRight.y = self.height - pkRight.height;
  79. // pkLeft.y = self.height - pkLeft.height;
  80. // self.layer.cornerRadius = self.height/2;
  81. self.layer.masksToBounds = YES;
  82. valueLeft = [[UILabel alloc] init];
  83. valueLeft.frame = CGRectMake(5, 0, 100, 20);
  84. valueLeft.textColor = [UIColor whiteColor];
  85. valueLeft.textAlignment = NSTextAlignmentLeft;
  86. valueLeft.font = [UIFont systemFontOfSize:14];
  87. [self addSubview:valueLeft];
  88. valueRight = [[UILabel alloc] init];
  89. valueRight.frame = CGRectMake(self.width - 100 - 5, 0, 100, 20);
  90. valueRight.textColor = [UIColor whiteColor];
  91. valueRight.textAlignment = NSTextAlignmentRight;
  92. valueRight.font = [UIFont systemFontOfSize:14];
  93. if(_leftIsMe == YES)
  94. {
  95. valueLeft.text = [NSString stringWithFormat:ASLocalizedString(@"我方:%f"),_rightValue];
  96. valueRight.text = [NSString stringWithFormat:ASLocalizedString(@"%f:对方"),_leftValue];
  97. }
  98. else
  99. {
  100. valueLeft.text = [NSString stringWithFormat:ASLocalizedString(@"对方:%f"),_rightValue];
  101. valueRight.text = [NSString stringWithFormat:ASLocalizedString(@"%f:我方"),_leftValue];
  102. }
  103. [self addSubview:valueRight];
  104. // labTimer = [[UILabel alloc] init];
  105. // labTimer.textColor = kWhiteColor;
  106. // labTimer.text = @"";
  107. // labTimer.font = [UIFont systemFontOfSize:12];
  108. // // _countDown = 60;
  109. // FWWeakify(self)
  110. // timer = [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer) {
  111. // FWStrongify(self)
  112. // if(self->_countDown == 0)
  113. // {
  114. // [self->timer invalidate];
  115. // self->timer = nil;
  116. // self->labTimer.text = [NSString stringWithFormat:ASLocalizedString(@"结束")];
  117. // // ctl=pk&act=punishment_time
  118. // NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  119. // [dict setObject:@"pk" forKey:@"ctl"];
  120. // [dict setObject:@"punishment_time" forKey:@"act"];
  121. // [dict setObject:[IMAPlatform sharedInstance].host.imUserId forKey:@"user_id"];
  122. // [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  123. // if ([responseJson toInt:@"status"] == 1) {
  124. // NSLog(@"%s\n%@",__func__,responseJson);
  125. // }
  126. // } FailureBlock:^(NSError *error) {
  127. // //do nothing
  128. // }];
  129. // }
  130. // else
  131. // {
  132. // self->_countDown--;
  133. // self->labTimer.text = [NSString stringWithFormat:ASLocalizedString(@"%d秒"),_countDown];
  134. // }
  135. // } repeats:YES];
  136. [valueLeft mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.left.equalTo(@5);
  138. make.top.equalTo(self);
  139. // valueLeft.frame = CGRectMake(5, 0, 100, 20);
  140. }];
  141. [valueRight mas_makeConstraints:^(MASConstraintMaker *make) {
  142. make.right.equalTo(@(-5));
  143. make.top.equalTo(self);
  144. }];
  145. pkImage = [[UIImageView alloc] init];
  146. pkImage.image = [UIImage imageNamed:@"JianBianVS"];
  147. [self addSubview:pkImage];
  148. pkImage.contentMode = UIViewContentModeScaleAspectFit;
  149. [pkImage mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.width.equalTo(@20);
  151. make.height.equalTo(@20);
  152. make.top.equalTo(self);
  153. make.bottom.equalTo(self);
  154. make.centerX.equalTo(self);
  155. // make.height.equalTo(30);
  156. }];
  157. leftResultView = [[UIImageView alloc]initWithFrame:CGRectZero];
  158. [self addSubview:leftResultView];
  159. [leftResultView mas_makeConstraints:^(MASConstraintMaker *make) {
  160. make.centerY.equalTo(valueLeft.mas_centerY);
  161. make.right.equalTo(self.mas_centerX).offset(-5);
  162. make.size.mas_equalTo(CGSizeMake(34, 20));
  163. }];
  164. rightResultView = [[UIImageView alloc]initWithFrame:CGRectZero];
  165. [self addSubview:rightResultView];
  166. [rightResultView mas_makeConstraints:^(MASConstraintMaker *make) {
  167. make.centerY.equalTo(valueLeft.mas_centerY);
  168. make.left.equalTo(self.mas_centerX).offset(5);
  169. make.size.mas_equalTo(CGSizeMake(34, 20));
  170. }];
  171. }
  172. - (void)switchToPunish:(int)time{
  173. //2020-1-4 pk结束
  174. NSDictionary *dic2=@{@"leftValue":[NSString stringWithFormat:@"%.f",_leftValue],
  175. @"rightValue":[NSString stringWithFormat:@"%.f",_rightValue]};
  176. [[NSNotificationCenter defaultCenter] postNotificationName:@"endpkview" object:dic2];
  177. if (_leftValue > _rightValue) {
  178. //左边胜利,右边失败
  179. leftResultView.image = [UIImage imageNamed:@"ic_live_pk_win"];
  180. rightResultView.image = [UIImage imageNamed:@"ic_live_pk_lose"];
  181. }else if (_leftValue < _rightValue){
  182. //左边失败,右边胜利
  183. leftResultView.image = [UIImage imageNamed:@"ic_live_pk_lose"];
  184. rightResultView.image = [UIImage imageNamed:@"ic_live_pk_win"];
  185. }else{
  186. //左边平局,右边平局
  187. leftResultView.image = [UIImage imageNamed:@"ic_live_pk_draw"];
  188. rightResultView.image = [UIImage imageNamed:@"ic_live_pk_draw"];
  189. }
  190. CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  191. animation.fromValue = [NSNumber numberWithDouble:0.25];
  192. animation.toValue = [NSNumber numberWithDouble:1];
  193. animation.duration= 0.25;
  194. animation.autoreverses= NO;
  195. [leftResultView.layer addAnimation:animation forKey:@"scale"];
  196. [rightResultView.layer addAnimation:animation forKey:@"scale"];
  197. }
  198. -(void)setLeftIsMe:(BOOL)leftIsMe
  199. {
  200. _leftIsMe = leftIsMe;
  201. valueLeft.text = [NSString stringWithFormat:ASLocalizedString(@"我方 %.0f"),_leftValue];
  202. valueRight.text = [NSString stringWithFormat:ASLocalizedString(@"%.0f 对方"),_rightValue];
  203. }
  204. -(void)setRightValue:(float)rightValue
  205. {
  206. _rightValue = rightValue;
  207. if (leftResultView.image || rightResultView.image) {
  208. return;
  209. }
  210. valueLeft.text = [NSString stringWithFormat:ASLocalizedString(@"我方:%.0f"),_leftValue];
  211. valueRight.text = [NSString stringWithFormat:ASLocalizedString(@"%.0f:对方"),_rightValue];
  212. //计算动画
  213. float pkwidth = 0;
  214. if(_leftValue > _rightValue)
  215. {
  216. float percentage = _leftValue/(_rightValue+_leftValue);
  217. pkwidth = self.width * percentage;
  218. // self.width/2 + self.width/2*percentage;
  219. }else if (_leftValue == _rightValue){
  220. pkwidth = self.width/2;
  221. }else{
  222. float percentage = _rightValue/(_rightValue+_leftValue);
  223. // pkwidth = self.width/2 - self.width/2*percentage;
  224. pkwidth = self.width * (1 - percentage);
  225. }
  226. if(isnan(pkwidth))
  227. {
  228. pkwidth = self.width/2;
  229. }
  230. [UIView animateWithDuration:0.8 animations:^{
  231. UIBezierPath *maskPath1;
  232. if (pkwidth == self.width) {
  233. maskPath1 = [UIBezierPath bezierPathWithRoundedRect:pkLeft.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(0,0)];//圆角大小
  234. }else{
  235. maskPath1 = [UIBezierPath bezierPathWithRoundedRect:pkLeft.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) cornerRadii:CGSizeMake(0,0)];//圆角大小
  236. }
  237. CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
  238. maskLayer1.frame = pkLeft.bounds;
  239. maskLayer1.path = maskPath1.CGPath;
  240. pkLeft.layer.mask = maskLayer1;
  241. pkLeft.frame = CGRectMake(0, 0, pkwidth, KPKRightOrLeftHeiht);
  242. // pkLeft.y = self.height - pkLeft.height;
  243. }];
  244. }
  245. -(void)setLeftValue:(float)leftValue
  246. {
  247. _leftValue = leftValue;
  248. if (leftResultView.image || rightResultView.image) {
  249. return;
  250. }
  251. // if(_leftIsMe == YES)
  252. // {
  253. // valueLeft.text = [NSString stringWithFormat:ASLocalizedString(@"我方: %.0f"),_leftValue];
  254. // valueRight.text = [NSString stringWithFormat:ASLocalizedString(@"对方: %.0f"),_rightValue];
  255. //
  256. // }
  257. // else
  258. // {
  259. // valueLeft.text = [NSString stringWithFormat:ASLocalizedString(@"对方: %.0f"),_rightValue];
  260. // valueRight.text = [NSString stringWithFormat:ASLocalizedString(@"我方: %.0f"),_leftValue];
  261. // }
  262. valueLeft.text = [NSString stringWithFormat:ASLocalizedString(@"我方:%.0f"),_leftValue];
  263. valueRight.text = [NSString stringWithFormat:ASLocalizedString(@"%.0f:对方"),_rightValue];
  264. //计算动画
  265. float pkwidth = 0;
  266. if(_leftValue > _rightValue)
  267. {
  268. float percentage = _leftValue/(_rightValue+_leftValue);
  269. // pkwidth = self.width/2 + self.width/2*percentage;
  270. pkwidth = self.width * percentage;
  271. }else if (_leftValue == _rightValue){
  272. pkwidth = self.width/2;
  273. }else
  274. {
  275. float percentage = _rightValue/(_rightValue+_leftValue);
  276. pkwidth = self.width * (1 - percentage);
  277. // self.width/2 - self.width/2*percentage;
  278. }
  279. if(isnan(pkwidth))
  280. {
  281. pkwidth = self.width/2;
  282. }
  283. // pkwidth = kScreenW -50;
  284. [UIView animateWithDuration:0.8 animations:^{
  285. pkLeft.frame = CGRectMake(0, 0, pkwidth, KPKRightOrLeftHeiht);
  286. // pkLeft.y = self.height - pkLeft.height;
  287. }];
  288. }
  289. - (void)setModel:(BogoPkProgressModel *)model{
  290. NSLog(@"%@",self.room_id);
  291. NSLog(@"model.gift_list1.firstObject%@",model.gift_list1.firstObject);
  292. self.pkAudienceView.room_id = self.room_id;
  293. self.pkAudienceView.model = model;
  294. }
  295. -(void)showPublishView{
  296. if (_leftValue > _rightValue) {
  297. //左边胜利,右边失败
  298. leftResultView.image = [UIImage imageNamed:@"ic_live_pk_win"];
  299. rightResultView.image = [UIImage imageNamed:@"ic_live_pk_lose"];
  300. }else if (_leftValue < _rightValue){
  301. //左边失败,右边胜利
  302. leftResultView.image = [UIImage imageNamed:@"ic_live_pk_lose"];
  303. rightResultView.image = [UIImage imageNamed:@"ic_live_pk_win"];
  304. }else{
  305. //左边平局,右边平局
  306. leftResultView.image = [UIImage imageNamed:@"ic_live_pk_draw"];
  307. rightResultView.image = [UIImage imageNamed:@"ic_live_pk_draw"];
  308. }
  309. CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  310. animation.fromValue = [NSNumber numberWithDouble:0.25];
  311. animation.toValue = [NSNumber numberWithDouble:1];
  312. animation.duration= 0.25;
  313. animation.autoreverses= NO;
  314. [leftResultView.layer addAnimation:animation forKey:@"scale"];
  315. [rightResultView.layer addAnimation:animation forKey:@"scale"];
  316. }
  317. -(BogoPkAudienceView *)pkAudienceView{
  318. if (!_pkAudienceView) {
  319. _pkAudienceView = [[BogoPkAudienceView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kRealValue(KPKAudienceHeiht))];
  320. }
  321. return _pkAudienceView;
  322. }
  323. - (void)dealloc{
  324. NSLog(@"%s",__func__);
  325. }
  326. @end