UITextView自动滚动的解决方案

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

 

posted @   王喆(nasa)  阅读(7125)  评论(3编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2008-10-14 Silverlight 2 RTW 已可以下载
2008-10-14 Silverlight 2 正式版官方正式发布
点击右上角即可分享
微信分享提示