Pergunta

In my project i implemented a ticker animation which scrolls text horizontally.

my problem is when i goes to another viewcontroller memory starts to increase constantly.

here is my code for ticker animation

-(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;
    }
}

memory goes like this:

enter image description here

please help me to solve this. any suggestions appreciated. thanks in advance

Foi útil?

Solução

OK, first thing... wow, you need to improve the code style.

Lemme have a go.

Second, stop using that old style animation code. The docs even say not to use it since 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;
    }
}

As for the memory issue. I'd suggest finding which part of the code is causing the issue by profiling the app using instruments.

You may find that this will improve the memory usage anyway? Maybe, but not 100% certain on that.

Outras dicas

Maybe I shouldn't be criticizing but that coding style is not good and it might create you lots of trouble in the future. I'm guessing you are trying to stop the animation if isTicker is set to false by possibly as a result of user actions.

Just check the value of isTicker once just before the animations. The intervals you are checking its value are so small either way

if (isTicker)
{
    float duration = (textSize.width + self.tickerView.frame.size.width) / 65.0f;
    float startingX=0.0f;
    float endX=0.0f;

    if (isTicker)

It will take your device some milliseconds to check the value of isTicker twice here for example.

You should definitely change the coding style, my guess for the increased memory consumption is the below snippet. You begin animations but then if isTicker is set to FALSE the animations are not committed, animation context is created but it is not finalized hence possibly still stored in memory.

[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];
                            }
                        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top