OCBarrageTextCell.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // OCBarrageTextCell.m
  3. // TestApp
  4. //
  5. // Created by QMTV on 2017/8/23.
  6. // Copyright © 2017年 LFC. All rights reserved.
  7. //
  8. #import "OCBarrageTextCell.h"
  9. @implementation OCBarrageTextCell
  10. - (instancetype)init {
  11. self = [super init];
  12. if (self) {
  13. }
  14. return self;
  15. }
  16. - (void)prepareForReuse {
  17. [super prepareForReuse];
  18. }
  19. - (void)updateSubviewsData {
  20. if (!_textLabel) {
  21. [self addSubview:self.textLabel];
  22. }
  23. if (self.textDescriptor.textShadowOpened) {
  24. self.textLabel.layer.shadowColor = self.textDescriptor.shadowColor.CGColor;
  25. self.textLabel.layer.shadowOffset = self.textDescriptor.shadowOffset;
  26. self.textLabel.layer.shadowRadius = self.textDescriptor.shadowRadius;
  27. self.textLabel.layer.shadowOpacity = self.textDescriptor.shadowOpacity;
  28. }
  29. [self.textLabel setAttributedText:self.textDescriptor.attributedText];
  30. }
  31. - (void)layoutContentSubviews {
  32. CGRect textFrame = [self.textDescriptor.attributedText.string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[self.textDescriptor.attributedText attributesAtIndex:0 effectiveRange:NULL] context:nil];
  33. self.textLabel.frame = textFrame;
  34. }
  35. - (void)convertContentToImage {
  36. UIImage *contentImage = [self.layer convertContentToImageWithSize:_textLabel.frame.size];
  37. [self.layer setContents:(__bridge id)contentImage.CGImage];
  38. }
  39. - (void)removeSubViewsAndSublayers {
  40. [super removeSubViewsAndSublayers];
  41. _textLabel = nil;
  42. }
  43. - (void)addBarrageAnimationWithDelegate:(id<CAAnimationDelegate>)animationDelegate {
  44. if (!self.superview) {
  45. return;
  46. }
  47. CGPoint startCenter = CGPointMake(CGRectGetMaxX(self.superview.bounds) + CGRectGetWidth(self.bounds)/2, self.center.y);
  48. CGPoint endCenter = CGPointMake(-(CGRectGetWidth(self.bounds)/2), self.center.y);
  49. CAKeyframeAnimation *walkAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  50. walkAnimation.values = @[[NSValue valueWithCGPoint:startCenter], [NSValue valueWithCGPoint:endCenter]];
  51. walkAnimation.keyTimes = @[@(0.0), @(1.0)];
  52. walkAnimation.duration = self.barrageDescriptor.animationDuration;
  53. walkAnimation.repeatCount = 1;
  54. walkAnimation.delegate = animationDelegate;
  55. walkAnimation.removedOnCompletion = NO;
  56. walkAnimation.fillMode = kCAFillModeForwards;
  57. [self.layer addAnimation:walkAnimation forKey:kBarrageAnimation];
  58. }
  59. - (UILabel *)textLabel {
  60. if (!_textLabel) {
  61. _textLabel = [[UILabel alloc] init];
  62. _textLabel.textAlignment = NSTextAlignmentCenter;
  63. }
  64. return _textLabel;
  65. }
  66. - (void)setBarrageDescriptor:(OCBarrageDescriptor *)barrageDescriptor {
  67. [super setBarrageDescriptor:barrageDescriptor];
  68. self.textDescriptor = (OCBarrageTextDescriptor *)barrageDescriptor;
  69. }
  70. @end