前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITextView自动滚动的解决方案

UITextView自动滚动的解决方案

作者头像
用户1172164
发布2018-01-16 12:04:47
1.5K0
发布2018-01-16 12:04:47
举报

思路:UIView加载完成3后,用NSTimer结合setContentOffset将UITextView向下滚动1像素。在滚动到底部的时候停止NSTimer。每当手动滚动UITextView前销毁NSTimer,滚动后重新创建NSTimer。

代码语言:javascript
复制
NSTimer *timer;
- (void)viewDidLoad {
	[self performSelector:@selector(resetText) withObject:nil afterDelay:3.0f];
}

- (void)resetText {	
	[timer invalidate];
	timer = nil;
	timer = [NSTimer scheduledTimerWithTimeInterval: 0.06
											 target: self
										   selector:@selector(onTick:)
										   userInfo: nil repeats:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
	[timer invalidate];
	timer = nil;
	NSLog(@"scrollViewWillBeginDragging");
	[self performSelector:@selector(resetText) withObject:nil afterDelay:3.0f];
}
- (void) onTick:(NSTimer*)theTimer {
	CGPoint pt = [textView contentOffset];
	CGFloat n = pt.y + 1;
	[textView setContentOffset:CGPointMake(pt.x, n)];	
	if (n> (textView.contentSize.height-textView.bounds.size.height)) {
		[theTimer invalidate];
		theTimer = nil;
		[timer invalidate];
		timer = nil;
	}
}

最后 不要忘了实现

UIScrollViewDelegate

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-10-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档