我正在尝试自动滚动文字视图,并在结束时将其重置为顶部。
我使用此代码:
-(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
值设置错误了吗?
编辑:
问题解决了!我坚持使用NSTimer - 缺少的密钥是为图层调用-display
。
-(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];
}
}
答案 0 :(得分:1)
您也可以直接使用CoreAnimation并为bounds属性设置动画。首先为滚动设置动画,然后在动画完成的委托回调中重置内容偏移。
回调方法必须具有签名
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
如果您的目标是iOS 4.0及更高版本,也可以使用新的基于块的方法。然后有两个块要传递:在第一个块中指定动画的内容,在第二个块中指定动画结束时的操作。
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
你的问题而不是折叠成一行代码:
[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%确定精确的块语法,但这是它应该如何工作的。