在ios中自动滚动的无限Textview

时间:2013-03-08 11:58:56

标签: ios uiscrollview uitextview

实施textview中的自动滚动。但我无法实现无限的textview。 我想将相同的文本追加到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];
}

1 个答案:

答案 0 :(得分:1)

您可以在达到初始正文文本高度后滚动回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

(在iPhone Retina 4英寸目标上试试这个,可能需要对iPad进行一些调整。)