首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIViewAnimation增加内存

UIViewAnimation增加内存
EN

Stack Overflow用户
提问于 2014-02-17 09:08:08
回答 2查看 119关注 0票数 0

在我的项目中,我实现了一个滴答动画,它横向滚动文本。

我的问题是当我进入另一个视图控制器内存时,内存就会不断增加。

这是我的代码代码

代码语言:javascript
复制
-(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;
    }
}

记忆是这样的:

请帮我解决这个问题。如有任何建议请见谅。提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-17 11:29:38

好吧,第一件事.哇,你需要改进代码风格。

让我试试。

第二,停止使用旧风格的动画代码。自iOS 4.0以来,文档甚至说不要使用它。

代码语言:javascript
复制
-(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;
    }
}

至于内存问题。我建议通过使用仪器来分析应用程序,找出代码的哪一部分是造成问题的原因。

你可能会发现这会提高内存的使用吗?也许吧,但不能百分之百肯定。

票数 1
EN

Stack Overflow用户

发布于 2014-02-17 11:12:22

也许我不应该批评,但这种编码方式不好,将来可能会给你带来很多麻烦。如果isTicker被设置为false,可能是用户操作的结果,那么我猜您正在尝试停止动画。

只需在动画之前检查一次isTicker的值。无论哪种方式,您正在检查其值的间隔都是如此之小。

代码语言:javascript
复制
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,动画没有提交,动画上下文将被创建,但尚未完成,因此可能仍然存储在内存中。

代码语言:javascript
复制
[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];
                            }
                        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21824802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档