SGHorseRaceLampView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // SGHorseRaceLampView.m
  3. // SGHorseRaceLampView
  4. //
  5. // Created by Sorgle on 2017/6/17.
  6. // Copyright © 2017年 Sorgle. All rights reserved.
  7. //
  8. // - - - - - - - - - - - - - - 交流QQ:1357127436 - - - - - - - - - - - - - - - //
  9. //
  10. // - - 如在使用中, 遇到什么问题或者有更好建议者, 请于 kingsic@126.com 邮箱联系 - - - - //
  11. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  12. // - - GitHub下载地址 https://github.com/kingsic/SGAnimation.git - — - - - - - //
  13. //
  14. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
  15. #import "SGHorseRaceLampView.h"
  16. @interface SGHorseRaceLampView ()
  17. @property (nonatomic, strong) UIImageView *imageView;
  18. @property (nonatomic, strong) UIView *contentView;
  19. @property (nonatomic, strong) UILabel *contentLabel;
  20. @end
  21. @implementation SGHorseRaceLampView
  22. - (instancetype)initWithFrame:(CGRect)frame {
  23. if (self = [super initWithFrame:frame]) {
  24. [self initialization];
  25. [self setupSubviews];
  26. }
  27. return self;
  28. }
  29. - (void)awakeFromNib {
  30. [super awakeFromNib];
  31. [self initialization];
  32. [self setupSubviews];
  33. }
  34. - (void)initialization {
  35. _scrollTimeInterval = 10;
  36. _titleFont = [UIFont systemFontOfSize:15];
  37. }
  38. - (void)setupSubviews {
  39. [self addSubview:self.imageView];
  40. [self addSubview:self.contentView];
  41. [_contentView addSubview:self.contentLabel];
  42. }
  43. - (void)layoutSubviews {
  44. [super layoutSubviews];
  45. CGFloat spacing = 10;
  46. CGFloat imageViewW = _imageView.image.size.width;
  47. CGFloat imageViewH = _imageView.image.size.height;
  48. if (imageViewH > self.frame.size.height) {
  49. imageViewH = self.frame.size.height - spacing;
  50. }
  51. CGFloat imageViewX = spacing;
  52. CGFloat imageViewY = 0.5 * (self.frame.size.height - imageViewH);
  53. _imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
  54. CGFloat contentViewX = CGRectGetMaxX(_imageView.frame) + 10;
  55. CGFloat contentViewY = 0;
  56. CGFloat contentViewW = self.frame.size.width - contentViewX - spacing;
  57. CGFloat contentViewH = self.frame.size.height;
  58. _contentView.frame = CGRectMake(contentViewX, contentViewY, contentViewW, contentViewH);
  59. CGFloat textWidth = [self SG_widthWithString:_contentLabel.text font:self.titleFont];
  60. CGFloat contentLabelX = 0;
  61. CGFloat contentLabelY = 0;
  62. CGFloat contentLabelW = textWidth + _contentView.frame.size.width;
  63. CGFloat contentLabelH = _contentView.frame.size.height;
  64. _contentLabel.frame = CGRectMake(contentLabelX, contentLabelY, contentLabelW, contentLabelH);
  65. if (textWidth > _contentView.frame.size.width) {
  66. [self addAnimationWithFirstContentLabel];
  67. }
  68. }
  69. - (CGFloat)SG_widthWithString:(NSString *)string font:(UIFont *)font {
  70. NSDictionary *attrs = @{NSFontAttributeName: font};
  71. CGFloat stringWidth = [string boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
  72. return stringWidth;
  73. }
  74. - (UIImageView *)imageView {
  75. if (!_imageView) {
  76. _imageView = [[UIImageView alloc] init];
  77. }
  78. return _imageView;
  79. }
  80. - (UIView *)contentView {
  81. if (!_contentView) {
  82. _contentView = [[UIView alloc] init];
  83. _contentView.clipsToBounds = YES;
  84. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapContentView)];
  85. [_contentView addGestureRecognizer:tap];
  86. }
  87. return _contentView;
  88. }
  89. - (UILabel *)contentLabel {
  90. if (!_contentLabel) {
  91. _contentLabel = [[UILabel alloc] init];
  92. }
  93. return _contentLabel;
  94. }
  95. - (void)tapContentView {
  96. if (self.selectedBlock) {
  97. self.selectedBlock();
  98. }
  99. }
  100. - (void)addAnimationWithFirstContentLabel {
  101. __weak typeof(self) weakSelf = self;
  102. [UIView animateWithDuration:self.scrollTimeInterval delay:1 options:(UIViewAnimationOptionCurveLinear) animations:^{
  103. CGFloat width = [weakSelf SG_widthWithString:_contentLabel.text font:self.titleFont];
  104. CGRect tempFrame = _contentLabel.frame;
  105. tempFrame.origin.x = - width;
  106. _contentLabel.frame = tempFrame;
  107. } completion:^(BOOL finished) {
  108. [weakSelf addAnimationWithContentLabel];
  109. }];
  110. }
  111. - (void)addAnimationWithContentLabel {
  112. CGFloat width = [self SG_widthWithString:_contentLabel.text font:self.titleFont];
  113. CGFloat contentLabelX = _contentView.frame.size.width;
  114. CGFloat contentLabelY = 0;
  115. CGFloat contentLabelW = width;
  116. CGFloat contentLabelH = _contentView.frame.size.height;
  117. _contentLabel.frame = CGRectMake(contentLabelX, contentLabelY, contentLabelW, contentLabelH);
  118. __weak typeof(self) weakSelf = self;
  119. [UIView animateWithDuration:self.scrollTimeInterval delay:0 options:(UIViewAnimationOptionCurveLinear) animations:^{
  120. CGRect tempFrame = _contentLabel.frame;
  121. tempFrame.origin.x = - width;
  122. _contentLabel.frame = tempFrame;
  123. } completion:^(BOOL finished) {
  124. [weakSelf addAnimationWithContentLabel];
  125. }];
  126. }
  127. - (void)setImageNage:(NSString *)imageNage {
  128. _imageNage = imageNage;
  129. if (imageNage) {
  130. _imageView.image = [UIImage imageNamed:imageNage];
  131. }
  132. }
  133. - (void)setTitle:(NSString *)title {
  134. _title = title;
  135. if (title) {
  136. _contentLabel.text = title;
  137. }
  138. }
  139. - (void)setTitleColor:(UIColor *)titleColor {
  140. _titleColor = titleColor;
  141. if (titleColor) {
  142. _contentLabel.textColor = titleColor;
  143. }
  144. }
  145. - (void)setTitleFont:(UIFont *)titleFont {
  146. _titleFont = titleFont;
  147. if (titleFont) {
  148. _contentLabel.font = titleFont;
  149. _titleFont = titleFont;
  150. }
  151. }
  152. - (void)setScrollTimeInterval:(CGFloat)scrollTimeInterval {
  153. _scrollTimeInterval = scrollTimeInterval;
  154. }
  155. @end