在我的项目中,我实现了一个水平滚动文本的自动收录器动画。
我的问题是当我去另一个viewcontroller内存时开始不断增加。
这是我的代码动画代码
-(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;
}
}
记忆是这样的:
请帮我解决这个问题。任何建议赞赏。提前谢谢
答案 0 :(得分:1)
好的,首先......哇,你需要改进代码风格。
Lemme快走了。
其次,停止使用旧式动画代码。自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;
}
}
关于记忆问题。我建议通过使用工具分析应用程序来找到导致问题的部分代码。
你可能会发现这会改善内存使用量吗?也许,但不是100%肯定。
答案 1 :(得分:0)
也许我不应该批评,但编码风格不好,可能会给你带来很多麻烦。我猜你正试图停止动画,如果isTicker被设置为假,可能是因为用户操作。
只需在动画之前检查一次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];
}
}