VerScrollTextView.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // VerScrollTextView.m
  3. // ScrollMarqueeView
  4. //
  5. // Created by admin on 2021/1/28.
  6. //
  7. #import "VerScrollTextView.h"
  8. #import <Masonry/Masonry.h>
  9. @interface VerScrollTextView ()
  10. @property (nonatomic, strong) NSTimer *timer;
  11. @end
  12. @implementation VerScrollTextView
  13. -(UIView *)verBgView{
  14. if (!_verBgView) {
  15. _verBgView=[UIView new];
  16. _verBgView.backgroundColor=[UIColor clearColor];
  17. _verBgView.layer.masksToBounds=YES;
  18. }
  19. return _verBgView;
  20. }
  21. -(UILabel *)txtLabel{
  22. if (!_txtLabel) {
  23. _txtLabel=[UILabel new];
  24. _txtLabel.font=[UIFont fontWithName:Rob_Regular size:14];
  25. _txtLabel.numberOfLines=0;
  26. _txtLabel.textAlignment = NSTextAlignmentCenter;
  27. _txtLabel.textColor=[UIColor blackColor];
  28. }
  29. return _txtLabel;
  30. }
  31. - (void)dealloc {
  32. [self.timer invalidate];
  33. self.timer = nil;
  34. }
  35. -(instancetype)init{
  36. self=[super init];
  37. if (self) {
  38. __weak typeof(self) weakSelf=self;
  39. [self addSubview:self.verBgView];
  40. [self.verBgView addSubview:self.txtLabel];
  41. [self.verBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.top.equalTo(weakSelf.mas_top).offset(10);
  43. make.leading.equalTo(weakSelf.mas_leading).offset(10);
  44. make.trailing.equalTo(weakSelf.mas_trailing).offset(-10);
  45. make.bottom.equalTo(weakSelf.mas_bottom).offset(-10);
  46. }];
  47. [self.txtLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  48. make.leading.trailing.top.equalTo(weakSelf.verBgView);
  49. make.bottom.equalTo(weakSelf.verBgView.mas_bottom);
  50. }];
  51. self.arrNum=0;
  52. }
  53. return self;
  54. }
  55. -(void)setSxArry:(NSMutableArray *)sxArry{
  56. _sxArry=sxArry;
  57. if (self.timer) {
  58. [_timer invalidate];
  59. self.timer = nil;
  60. }
  61. if (sxArry.count == 0) {
  62. return;
  63. }
  64. if (self.arrNum >= sxArry.count) {
  65. self.arrNum = 0;
  66. }
  67. @weakify(self);
  68. self.timer = [NSTimer scheduledTimerWithTimeInterval:2 repeats:true block:^(NSTimer * _Nonnull timer) {
  69. [weak_self scrollTxtAction];
  70. }];
  71. }
  72. -(void)checkDataIndex{
  73. if (self.arrNum==self.sxArry.count-1) {
  74. self.arrNum=0;
  75. }else{
  76. self.arrNum=self.arrNum+1;
  77. }
  78. }
  79. - (void)scrollTxtAction {
  80. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  81. CATransition *transition = [CATransition animation];
  82. // transition.duration = 0.3;
  83. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
  84. transition.type = kCATransitionPush;
  85. transition.subtype = kCATransitionFromTop;
  86. transition.delegate = self;
  87. [self.txtLabel.layer addAnimation:transition forKey:nil];
  88. [self checkDataIndex];
  89. } completion:^(BOOL finished) {
  90. if (self.arrNum >= self.sxArry.count) {
  91. return;
  92. }
  93. self.txtLabel.text=self.sxArry[self.arrNum];
  94. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  95. //
  96. // [self scrollTxtAction];
  97. //
  98. // });
  99. }];
  100. }
  101. @end