自动滚动UITextView问题 [英] Auto Scrolling UITextView Problem
问题描述
我试图自动滚动文本视图,并在到达末尾时将其重置为顶部。
I am trying to auto scroll my text view and reset it to the top once it's arrived at the end.
我使用以下代码:
-(void)scrollTextView
{
CGPoint scrollPoint = stationInfo.contentOffset;
scrollPoint = CGPointMake(scrollPoint.x, scrollPoint.y + 2);
if (scrollPoint.y == originalPoint.y + 100)
{
NSLog(@"Reset it");
scrollPoint = CGPointMake(originalPoint.x, originalPoint.y);
[stationInfo setContentOffset:scrollPoint animated:YES];
[scroller invalidate];
scroller = nil;
scroller = [NSTimer
scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(scrollTextView)
userInfo:nil
repeats:YES];
}
else
{
[stationInfo setContentOffset:scrollPoint animated:YES];
}
}
因此,文本视图跳来跳去,但我不知道为什么。是否有更好的方法来检测文本视图位于底部?我是否将 scrollPoint
值设置错误?
As a result, the text view jumps around wildly, but I don't quite know why. Is there maybe a better way of detecting that the text view is at the bottom? Am I setting the the scrollPoint
value wrong?
编辑:
问题已解决!我坚持使用NSTimer-缺少的键正在调用 -display
来获取图层。
ISSUE SOLVED! I stuck to NSTimer - the missing key was calling -display
for the layer.
-(void)scrollTextView
{
//incrementing the original point to get movement
originalPoint = CGPointMake(0, originalPoint.y + 2);
//getting the bottom
CGPoint bottom = CGPointMake(0, [stationInfo contentSize].height);
//comparing the two to detect a reset
if (CGPointEqualToPoint(originalPoint,bottom) == YES)
{
NSLog(@"Reset");
//killing the timer
[scroller invalidate];
scroller == nil;
//setting the reset point
CGPoint resetPoint = CGPointMake(0, 0);
//reset original point
originalPoint = CGPointMake(0, 0);
//reset the view.
[stationInfo setContentOffset:resetPoint animated:YES];
//force display
[stationInfo.layer display];
scroller = [NSTimer
scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(scrollTextView)
userInfo:nil
repeats:YES];
}
else
{
[stationInfo setContentOffset:originalPoint animated:YES];
}
}
推荐答案
您还可以使用CoreAnimation并直接为bounds属性设置动画。首先对滚动进行动画处理,然后在动画完成的委托回调中重置内容偏移。
You could also use CoreAnimation and animate the bounds property directly. First animate the scrolling, then in the delegate callback that the animation has finished you reset the content offset.
回调方法必须具有签名
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
如果您的目标是iOS 4.0及更高版本,则也可以使用基于块的新方法。然后有两个要传递的块:在第一个中,指定要动画的内容,在第二个中,指定动画结束时的操作。
You could also use the new block-based methods if you are targeting iOS 4.0 and above. Then there are two blocks to be passed: in the first one you specify what to animate and in the second what to do when the animation is over.
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
您遇到的问题会崩溃成一行代码:
Your problem than collapses into a single line of code:
[stationInfo animateWithDuration:10.f delay:0.f options:0 animations:^{
[stationInfo setContentOffset:CGPointMake(0, [stationInfo contentSize].height)];
} completion:^(BOOL finished){
if (finished) [stationInfo setContentOffset:CGPointMake(0,0)];
}];
说实话,我不确定100%的精确块语法,但这是应该如何工作的
To be honest I am not 100% sure about the precise block syntax but this is how it should work.
这篇关于自动滚动UITextView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!