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 进行一些调整。)