在我的项目中,我实现了一个水平滚动文本的股票动画。
我的问题是当我转到另一个 View Controller 时内存开始不断增加。
这是我的股票动画代码
-(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;
}
}
内存是这样的:
请帮我解决这个问题。任何建议表示赞赏。提前致谢
最佳答案
好的,首先...哇,您需要改进代码风格。
让我试一试。
其次,停止使用旧式动画代码。文档甚至说从 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% 确定这一点。
关于ios - UIView动画增加内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21824802/