YOUPAIOCBarrageTextCell.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // YOUPAIOCBarrageTextCell.m
  3. // TestApp
  4. //
  5. // Created by QMTV on 2017/8/23.
  6. // Copyright © 2017年 LFC. All rights reserved.
  7. //
  8. #import "YOUPAIOCBarrageTextCell.h"
  9. @implementation YOUPAIOCBarrageTextCell
  10. - (instancetype)init {
  11. self = [super init];
  12. if (self) {
  13. }
  14. return self;
  15. }
  16. - (void)prepareForReuse {
  17. [super prepareForReuse];
  18. }
  19. - (void)youpaifupdateSubviewsData {
  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)youpaiflayoutContentSubviews {
  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)youpaifconvertContentToImage {
  36. UIImage *contentImage = [self.layer youpaifconvertContentToImageWithSize:_textLabel.frame.size];
  37. [self.layer setContents:(__bridge id)contentImage.CGImage];
  38. }
  39. - (void)removeSubViewsAndSublayers {
  40. [super removeSubViewsAndSublayers];
  41. _textLabel = nil;
  42. }
  43. - (void)youpaifaddBarrageAnimationWithDelegate:(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. CGFloat animationDuration = self.barrageDescriptor.animationDuration;
  50. if (self.barrageDescriptor.fixedSpeed > 0.0) {//如果是固定速度那就用固定速度
  51. if (self.barrageDescriptor.fixedSpeed > 100.0) {
  52. self.barrageDescriptor.fixedSpeed = 100.0;
  53. }
  54. animationDuration = (startCenter.x - endCenter.x)/([UIScreen mainScreen].scale*2)/self.barrageDescriptor.fixedSpeed;
  55. }
  56. CAKeyframeAnimation *walkAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  57. walkAnimation.values = @[[NSValue valueWithCGPoint:startCenter], [NSValue valueWithCGPoint:endCenter]];
  58. walkAnimation.keyTimes = @[@(0.0), @(1.0)];
  59. walkAnimation.duration = animationDuration;
  60. walkAnimation.repeatCount = 1;
  61. walkAnimation.delegate = animationDelegate;
  62. walkAnimation.removedOnCompletion = NO;
  63. walkAnimation.fillMode = kCAFillModeForwards;
  64. [self.layer addAnimation:walkAnimation forKey:kBarrageAnimation];
  65. }
  66. - (UILabel *)textLabel {
  67. if (!_textLabel) {
  68. _textLabel = [[UILabel alloc] init];
  69. _textLabel.textAlignment = NSTextAlignmentCenter;
  70. }
  71. return _textLabel;
  72. }
  73. - (void)setBarrageDescriptor:(YOUPAIOCBarrageDescriptor *)barrageDescriptor {
  74. [super setBarrageDescriptor:barrageDescriptor];
  75. self.textDescriptor = (YOUPAIOCBarrageTextDescriptor *)barrageDescriptor;
  76. }
  77. @end