years.text = [NSString stringWithFormat:@"%0.1f", theScroller.contentOffset.y];
将避免显式释放字符串的需要,因为它是自动释放的。
但是,如果您想避免减速,请考虑减少更新字段的频率。例如,每次scrollViewDidScroll
调用时,设置一个计时器以从现在开始在 0.1 秒内更新该字段,但如果计时器已经从先前的调用运行,则不会。这样可以减少调用次数,同时保持 UI 更新。
这是一个示例,您可以如何做到这一点。NSTimer
在滚动视图委托的接口声明中声明:
NSTimer *timer;
和方法:
- (void)updateYear:(NSTimer*)theTimer
{
timer=nil;
UIScrollView *theScroller=[theTimer userInfo];
years.text=[NSString stringWithFormat:@"%0.1f", theScroller.contentOffset.y];
}
- (void)scrollViewDidScroll:(UIScrollView *)theScroller
{
if (!timer) {
timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateYear:) userInfo:theScroller repeats:NO];
}
}
显然,您不必将其0.1
用作时间间隔,您可以尝试使其更快或更慢,看看哪种效果最好。
请注意,就内存管理而言,此示例是完整的,您不应尝试自己保留或释放计时器对象。它的生命周期由运行循环在内部处理。