在我的项目中,我实现了一个滴答动画,它横向滚动文本。
我的问题是当我进入另一个视图控制器内存时,内存就会不断增加。
这是我的代码代码
-(void)scrollTheBreakingNews
{
if (isTicker)
{
self.ticker.text = textToScroll;
if (!pauseTicker)
{
if (isTicker)
{
NSAttributedString *str = [[NSAttributedString alloc]initWithString:textToScroll];
CGSize textSize = [str size];
if (isTicker)
{
float duration = (textSize.width + self.tickerView.frame.size.width) / 65.0f;
float startingX=0.0f;
float endX=0.0f;
if (isTicker)
{
self.ticker.frame = scrollLabelFrame;
if (isTicker)
{
startingX = self.tickerView.frame.size.width;
endX = -textSize.width;
if (isTicker)
{
self.ticker.frame = CGRectMake(startingX, 0.0f, textSize.width, 25.0f);
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:duration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(tickerStop)];
if (isTicker)
{
CGRect tickerFrame = self.ticker.frame;
tickerFrame.origin.x = endX;
if (isTicker)
{
[self.ticker setFrame:tickerFrame];
[UIView commitAnimations];
}
}
}
}
}
}
}
}
}
}
-(void)tickerStop
{
if (isTicker)
{
if (!pauseTicker)
{
[self scrollTheBreakingNews];
}
}
else
{
textToScroll=nil;
}
}记忆是这样的:

请帮我解决这个问题。如有任何建议请见谅。提前感谢
发布于 2014-02-17 11:29:38
好吧,第一件事.哇,你需要改进代码风格。
让我试试。
第二,停止使用旧风格的动画代码。自iOS 4.0以来,文档甚至说不要使用它。
-(void)scrollTheBreakingNews
{
//You are already checking isTicker here there is
//no reason to check it another SEVEN times inside this block.
if (isTicker)
{
self.ticker.text = textToScroll;
if (!pauseTicker)
{
NSAttributedString *str = [[NSAttributedString alloc] initWithString:textToScroll];
CGSize textSize = [str size];
float duration = (textSize.width + self.tickerView.frame.size.width) / 65.0f;
float startingX=0.0f;
float endX=0.0f;
self.ticker.frame = scrollLabelFrame;
startingX = self.tickerView.frame.size.width;
endX = -textSize.width;
self.ticker.frame = CGRectMake(startingX, 0.0f, textSize.width, 25.0f);
CGRect tickerFrame = self.ticker.frame;
tickerFrame.origin.x = endX;
[UIView animateWithDuration:duration
delay:0.0
options:UIViewAnimationOptionsCurveLinear
animations:^(){
self.ticker.frame = tickerFrame
}
completion:^(BOOL finished){
[self tickerStop];
}];
}
}
}
-(void)tickerStop
{
if (!pauseTicker
&& isTicker) {
[self scrollTheBreakingNews];
}
else {
textToScroll=nil;
}
}至于内存问题。我建议通过使用仪器来分析应用程序,找出代码的哪一部分是造成问题的原因。
你可能会发现这会提高内存的使用吗?也许吧,但不能百分之百肯定。
发布于 2014-02-17 11:12:22
也许我不应该批评,但这种编码方式不好,将来可能会给你带来很多麻烦。如果isTicker被设置为false,可能是用户操作的结果,那么我猜您正在尝试停止动画。
只需在动画之前检查一次isTicker的值。无论哪种方式,您正在检查其值的间隔都是如此之小。
if (isTicker)
{
float duration = (textSize.width + self.tickerView.frame.size.width) / 65.0f;
float startingX=0.0f;
float endX=0.0f;
if (isTicker)例如,您的设备将花费几毫秒时间来检查isTicker的值两次。
您一定要改变编码风格,我猜增加内存消耗的原因是下面的代码片段。您开始动画,但是如果isTicker设置为FALSE,动画没有提交,动画上下文将被创建,但尚未完成,因此可能仍然存储在内存中。
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:duration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(tickerStop)];
if (isTicker)
{
CGRect tickerFrame = self.ticker.frame;
tickerFrame.origin.x = endX;
if (isTicker)
{
[self.ticker setFrame:tickerFrame];
[UIView commitAnimations];
}
}https://stackoverflow.com/questions/21824802
复制相似问题