WFCFavoriteSoundCell.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // WFCFavoriteUnknownCell.m
  3. // WildFireChat
  4. //
  5. // Created by Tom Lee on 2020/11/1.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCFavoriteSoundCell.h"
  9. #import <WFChatUIKit/WFChatUIKit.h>
  10. @interface WFCFavoriteSoundCell ()
  11. @property(nonatomic, strong)UIImageView *iconView;
  12. @property(nonatomic, strong)UILabel *nameLabel;
  13. @property(nonatomic, strong)NSTimer *animateTimeer;
  14. @end
  15. @implementation WFCFavoriteSoundCell
  16. - (void)awakeFromNib {
  17. [super awakeFromNib];
  18. // Initialization code
  19. }
  20. - (void)setFavoriteItem:(WFCUFavoriteItem *)favoriteItem {
  21. [super setFavoriteItem:favoriteItem];
  22. [self iconView];
  23. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[favoriteItem.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
  24. int duration = [dict[@"duration"] intValue];
  25. self.nameLabel.text = [NSString stringWithFormat:@"%d 秒", duration];
  26. }
  27. + (CGFloat)contentHeight:(WFCUFavoriteItem *)favoriteItem {
  28. return 56;
  29. }
  30. - (void)setIsPlaying:(BOOL)isPlaying {
  31. _isPlaying = isPlaying;
  32. if (isPlaying) {
  33. __weak typeof(self)ws = self;
  34. if (@available(iOS 10.0, *)) {
  35. self.animateTimeer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
  36. if((NSUInteger)[NSDate date].timeIntervalSince1970 % 2) {
  37. ws.iconView.image = nil;
  38. } else {
  39. ws.iconView.image = [UIImage imageNamed:@"sound_icon"];
  40. }
  41. }];
  42. } else {
  43. // Fallback on earlier versions
  44. }
  45. } else {
  46. [self.animateTimeer invalidate];
  47. self.animateTimeer = nil;
  48. self.iconView.image = [UIImage imageNamed:@"sound_icon"];
  49. }
  50. }
  51. - (UIImageView *)iconView {
  52. if (!_iconView) {
  53. _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 56, 56)];
  54. _iconView.image = [UIImage imageNamed:@"sound_icon"];
  55. _iconView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.f];
  56. [self.contentArea addSubview:_iconView];
  57. }
  58. return _iconView;
  59. }
  60. - (UILabel *)nameLabel {
  61. if (!_nameLabel) {
  62. _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(64, 8, self.bounds.size.width-72, 20)];
  63. _nameLabel.font = [UIFont systemFontOfSize:18];
  64. [self.contentArea addSubview:_nameLabel];
  65. }
  66. return _nameLabel;
  67. }
  68. @end