ios中具有自动滚动功能的无限文本视图

ios中具有自动滚动功能的无限文本视图,ios,uiscrollview,uitextview,Ios,Uiscrollview,Uitextview,实现了文本视图中的自动滚动。但我无法实现无限文本视图。 我想将相同的文本附加到textview并无限滚动 无限卷轴的代码 -(void)viewWillAppear:(BOOL)animated{ if (scrollingTimer == nil) { scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)

实现了文本视图中的自动滚动。但我无法实现无限文本视图。 我想将相同的文本附加到textview并无限滚动

无限卷轴的代码

-(void)viewWillAppear:(BOOL)animated{
    if (scrollingTimer == nil) {
        scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
                                                          target:self
                                                        selector:@selector(autoscrollTimerFired)
                                                        userInfo:nil
                                                         repeats:YES];
    }
    self.textView.contentOffset=CGPointMake(0, -(self.textView.frame.size.height));
}

- (void) autoscrollTimerFired{
    CGPoint scrollPoint = self.textView.contentOffset;
    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1);
    [self.textView setContentOffset:scrollPoint animated:NO];
}

达到初始正文高度后,您可以通过回滚到
UITextView
的开头来模拟此操作

下面是一个“无限”滚动随机Lorem段落的示例

它将该段落复制三次,以确保它看起来像是永远滚动

@implementation ViewController
{
    UITextView *infiniteTextView;
    NSTimer *scrollTimer;

    CGFloat textHeight;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor darkGrayColor];

    // Create a text view
    infiniteTextView = [[UITextView alloc] initWithFrame:CGRectInset(self.view.bounds, 0, self.view.bounds.size.height/4)];

    NSString *lorem = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n\n";

    infiniteTextView.font = [UIFont systemFontOfSize:15];

    CGRect textBounds = [lorem boundingRectWithSize:CGSizeMake(infiniteTextView.bounds.size.width - 16.0f, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName : infiniteTextView.font } context:nil];

    textHeight = textBounds.size.height;

    //Duplicate the paragraph 3 times
    infiniteTextView.text = [lorem stringByAppendingString:[lorem stringByAppendingString:lorem]];

    [self.view addSubview:infiniteTextView];
}

- (void) viewDidAppear:(BOOL)animated
{
    if (scrollTimer == nil)
    {
        scrollTimer = [NSTimer scheduledTimerWithTimeInterval:(0.06)
                                                          target:self
                                                        selector:@selector(autoscrollTimerFired:)
                                                        userInfo:nil
                                                         repeats:YES];
    }
}

- (void) autoscrollTimerFired:(id)sender
{
    CGPoint scrollPoint = infiniteTextView.contentOffset;

    //Reset the scroll position if we exceed
    // the body paragraph height
    if( scrollPoint.y > textHeight )
    {
        scrollPoint.y = 0;
    }

    scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 1);
    [infiniteTextView setContentOffset:scrollPoint animated:NO];
}

@end
(在iPhoneRetina4英寸的目标上试试这个,它可能需要为iPad做一些调整。)