ios开发文字上下滚动iOS学习专题

iOS文字滚动效果 之 纵向滚动

2016-07-14  本文已影响6199人  JerryLMJ

实现效果:

从下到上无间隙滚动 从上到下无间隙滚动 从下到上有间隙滚动 从上到下有间隙滚动

Demo地址:https://github.com/MajorLMJ/LMJScrollTextView2
如果此demo帮助到你,请赐给一颗star,你的鼓励是我coding的动力

这个模块支持设置滚动方向(从上到下、从下到上)、文字颜色大小位置以及停留滚动时间的设置。

2018.5.29更新(2.2.2):
本次更新修复了在xib或storyboard中使用时出现了多个字条的显示错误;
优化Demo演示效果,并添加了在storyboard中使用本控件的演示。
2018.5.16更新(2.2.0):
本次更新支持了文字停留、滚动时间的设置以及修复bug:
(1)支持设置文字停留的时间,默认为3s
(2)支持设置文字动画单次滚动时间,默认为1s
(3)修复文字滚动时点击无法响应
2018.1.3更新(2.1.0):
本次更新在数据源textDataArr中支持了NSAttributedString类型数据,使能够显示的文字类型更加丰富。
2017.12.28更新(2.0.0):
本次更新在原有的基础上增加更多滚动方式、属性设置以及回调方法:
(1)由原来的有间隙滚动,增加到有间隙和无间隙两种滚动方式。方法分别是startScrollBottomToTopWithSpace、startScrollTopToBottomWithSpace、startScrollBottomToTopWithNoSpace、startScrollTopToBottomWithNoSpace
(2)增加了文字的对齐方式和是否可以响应点击设置
(3)增加了点击回调代理函数
2017.8.18更新(1.3.5):
修复了反复调用startScrollBottomToTop / startScrollTopToBottom方法会产生异常效果的问题。
2017.8.16更新(1.3.2):
在demo中添加了把控件添加到cell上的测试。
2017.7.29更新(1.3.1):
修复app进入后台后,循环速度剧增、消耗大量cpu的的问题。
2017.2.14更新(1.3.0):
修复进入其他页面后,循环速度剧增、消耗大量cpu的的问题。
2017.2.13更新(1.2.0):
添加了一个简单的代理回调,用于返回当前信息的序号。
2017.2.13更新(1.1.0):
有朋友说第一条数据走了两次,后来查找一下原因发现是定时器的问题。
修改后取消使用定时器,改用循环调用实现。

欢迎大家在评论区提出在使用中遇到的问题,博主会及时修复大家提出的问题!

主要实现:

#pragma mark - Scroll Action
- (void)scrollWithNoSpaceByDirection:(NSNumber *)direction{
    // 处于非当前页面,延迟尝试
    if (![self isCurrentViewControllerVisible:[self viewController]]) {
        [self performSelector:@selector(scrollWithNoSpaceByDirection:) withObject:direction afterDelay:DelayCallTime];
        
        
    // 处于当前页面
    }else{
        if (_textDataArr.count == 0) {
            _isRunning = NO;
            return;
        }else{
            _isRunning = YES;
        }
        
        _currentScrollLabel.lmj_text  = _textDataArr[_index];
        _standbyScrollLabel.lmj_text  = _textDataArr[[self nextIndex:_index]];
        _standbyScrollLabel.frame = CGRectMake(0, self.lmj_height*direction.integerValue, _standbyScrollLabel.lmj_width, _standbyScrollLabel.lmj_height);
        
        _index = [self nextIndex:_index];
        
        [UIView animateWithDuration:ScrollItemTime animations:^{
            
            _currentScrollLabel.frame = CGRectMake(0, -self.lmj_height*direction.integerValue, _currentScrollLabel.lmj_width, _currentScrollLabel.lmj_height);
            _standbyScrollLabel.frame = CGRectMake(0, 0, _standbyScrollLabel.lmj_width, _standbyScrollLabel.lmj_height);
            
        } completion:^(BOOL finished) {
            
            if ([self isCurrentViewControllerVisible:[self viewController]] && self.delegate && [self.delegate respondsToSelector:@selector(scrollTextView2:currentTextIndex:)]) { // 代理回调
                [self.delegate scrollTextView2:self currentTextIndex:_index];
            }
            
            UILabel * temp = _currentScrollLabel;
            _currentScrollLabel = _standbyScrollLabel;
            _standbyScrollLabel = temp;
            
            if (_needStop == NO) {
                [self performSelector:@selector(scrollWithNoSpaceByDirection:) withObject:direction afterDelay:ScrollItemTime];
            }else{
                _isRunning = NO;
            }
        }];
    }
}

- (void)scrollWithSpaceByDirection:(NSNumber *)direction{
    
    // 处于非当前页面,延迟尝试
    if (![self isCurrentViewControllerVisible:[self viewController]]) {
        [self performSelector:@selector(scrollWithSpaceByDirection:) withObject:direction afterDelay:DelayCallTime];
   
    // 处于当前页面
    }else{
        if (_textDataArr.count == 0) {
            _isRunning = NO;
            return;
        }else{
            _isRunning = YES;
        }
        
        _currentScrollLabel.lmj_text  = _textDataArr[_index];
        _currentScrollLabel.frame = CGRectMake(0, 0, _currentScrollLabel.lmj_width, _currentScrollLabel.lmj_height);
        
        [UIView animateWithDuration:ScrollItemTime animations:^{
            _currentScrollLabel.frame = CGRectMake(0, -self.lmj_height*direction.integerValue, _currentScrollLabel.lmj_width, _currentScrollLabel.lmj_height);
            
        } completion:^(BOOL finished) {
            
            _currentScrollLabel.frame = CGRectMake(0, self.lmj_height*direction.integerValue, _currentScrollLabel.lmj_width, _currentScrollLabel.lmj_height);
            _index = [self nextIndex:_index];
            _currentScrollLabel.lmj_text  = _textDataArr[_index];
            
            if ([self isCurrentViewControllerVisible:[self viewController]] && self.delegate && [self.delegate respondsToSelector:@selector(scrollTextView2:currentTextIndex:)]) { // 代理回调
                [self.delegate scrollTextView2:self currentTextIndex:_index];
            }
            
            
            [UIView animateWithDuration:ScrollItemTime animations:^{
                _currentScrollLabel.frame = CGRectMake(0, 0, _currentScrollLabel.lmj_width, _currentScrollLabel.lmj_height);
                
            } completion:^(BOOL finished) {
                
                if (_needStop == NO) {
                    [self performSelector:@selector(scrollWithSpaceByDirection:) withObject:direction afterDelay:ScrollItemTime];
                }else{
                    _isRunning = NO;
                }
            }];
        }];
    }
}

Demo地址:
https://github.com/MajorLMJ/LMJScrollTextView2
如果此demo帮助到你,请赐给一颗star,你的鼓励是我coding的动力

版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!

上一篇下一篇

猜你喜欢

热点阅读