Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:35:31+00:00 2026-05-25T17:35:31+00:00

I have a recursive function that plays a UIView animation continuously (if there is

  • 0

I have a recursive function that plays a UIView animation continuously (if there is a better way to do this, please let me know):

-(void)playAnimationRecursive
{
    [appDelegate commenceFishAnimation];
    [animationView addSubview:appDelegate.fishAnimationImageView];
    animationView.alpha=0.5;

    [NSTimer scheduledTimerWithTimeInterval:14.0 target:self selector:@selector(playAnimationRecursive) userInfo:nil repeats:NO];
}

and not sure if this is neccessary, but here’s the appDelegate commenceFishAnimationMethod:

        upperFish=[[UIImageView alloc] initWithImage:upperFishImage];
        bigFish=[[UIImageView alloc] initWithImage:bigFishImage];
        shark=[[UIImageView alloc] initWithImage:sharkImage];
        groupFish=[[UIImageView alloc] initWithImage:groupFishImage];
        fish1=[[UIImageView alloc] initWithImage:fish1Image];
        fishAnimationImageView=[[[UIImageView alloc] init] retain];

        //set the initial position of each fish to be out of frame
        upperFish.frame=CGRectMake(-50, 150, 119/2, 93/2); //moves east
        bigFish.frame=CGRectMake(-280, 340, 251/2, 137/2); //moves east
        shark.frame=CGRectMake(-100, 390, 164/2, 52/2); //moves east
        groupFish.frame=CGRectMake(500, 320, 155/2, 89/2); //moves west
        fish1.frame=CGRectMake(370, 280, 155/2, 89/2); //moves west

        //add fishes to current view
        [fishAnimationImageView addSubview:upperFish];
        [fishAnimationImageView addSubview:bigFish];
        [fishAnimationImageView addSubview:shark];
        [fishAnimationImageView addSubview:groupFish];
        [fishAnimationImageView addSubview:fish1];

        //animate the position of each fish view
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:10.0];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        upperFish.transform=CGAffineTransformMakeTranslation(150, -200);
        bigFish.transform=CGAffineTransformMakeTranslation(600, 0);
        shark.transform=CGAffineTransformMakeTranslation(550, 0);
        groupFish.transform=CGAffineTransformMakeTranslation(-600, 0);
        fish1.transform=CGAffineTransformMakeTranslation(-550, 0);

        [UIView commitAnimations];
        [fishAnimationImageView release];
        [upperFish release];
        [bigFish release];
        [shark release];
        [groupFish release];
        [fish1 release];  
}

So what happens is that every view I have plays this animation in the BG, so if I’m on view A, then it calls the recursive function and plays the animation continuously. If I move to view B, then it calls the recursive function also and plays the animation continuously. However, I’m getting memory warning level 2, and I’m thinking maybe it’s because view A is still calling the recursive method? If that is the reason, then how would I stop the recursive method on unload? Or is that not my problem?

EDIT:

Here’s my code now (animation doesn’t loop)

-(void)commenceFishAnimation
{
    UIImageView *newImageView=[[UIImageView alloc] init];
    self.fishAnimationImageView=newImageView;

    upperFish=[[UIImageView alloc] initWithImage:upperFishImage];
    bigFish=[[UIImageView alloc] initWithImage:bigFishImage];
    shark=[[UIImageView alloc] initWithImage:sharkImage];
    groupFish=[[UIImageView alloc] initWithImage:groupFishImage];
    fish1=[[UIImageView alloc] initWithImage:fish1Image];

    //set the initial position of each fish to be out of frame
    upperFish.frame=CGRectMake(-50, 150, 119/2, 93/2); //moves east
    bigFish.frame=CGRectMake(-280, 340, 251/2, 137/2); //moves east
    shark.frame=CGRectMake(-100, 390, 164/2, 52/2); //moves east
    groupFish.frame=CGRectMake(500, 320, 155/2, 89/2); //moves west
    fish1.frame=CGRectMake(370, 280, 155/2, 89/2); //moves west

    //add fishes to current view
    [fishAnimationImageView addSubview:upperFish];
    [fishAnimationImageView addSubview:bigFish];
    [fishAnimationImageView addSubview:shark];
    [fishAnimationImageView addSubview:groupFish];
    [fishAnimationImageView addSubview:fish1];

    //animate the position of each fish view
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:10.0];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    upperFish.transform=CGAffineTransformMakeTranslation(150, -200);
    bigFish.transform=CGAffineTransformMakeTranslation(600, 0);
    shark.transform=CGAffineTransformMakeTranslation(550, 0);
    groupFish.transform=CGAffineTransformMakeTranslation(-600, 0);
    fish1.transform=CGAffineTransformMakeTranslation(-550, 0);

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(commenceFishAnimation)];
    [UIView commitAnimations]; 

    [fishAnimationImageView release];
    [upperFish release];
    [bigFish release];
    [shark release];
    [groupFish release];
    [fish1 release];
    //[newImageView release]; //if I release it, then my animation doesn't play for some reason
}

Then in some other class in the viewDidLoad method, I do this:

[appDelegate commenceFishAnimation];
[animationView addSubview:appDelegate.fishAnimationImageView];

But the animation only plays once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:35:32+00:00Added an answer on May 25, 2026 at 5:35 pm

    That’s not the best way chaining animations, use this instead:

        [UIView setAnimationDidStopSelector:@selector(playAnimation)];
    

    That will chain your animations in an infinite loop.

    But also note that in your method playAnimationRecursive you are adding subviews and scheduling a NSTimer every 14 seconds without releasing them. Thats a really bad idea.

    To sum up: forget about recursion and use the method above.

    EDIT: I forgot to mention that you have to assign the delegate of the animation:

    [UIView setAnimationDelegate:self];  //self or whatever implements playAnimation selector
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a recursive function that I want to know how many times
I have a simple recursive array function that looks like this: function recursive_array($results) {
I have a recursive jQuery function that has something like this: parentItem.find('> div:first-child >
I have a simple recursive function RCompare() that calls a more complex function Compare()
Is it possible to have a PHP function that is both recursive and anonymous?
So I have built a recursive function that generates a collection of Category objects.
If you have a recursive function that relies on some other function what is
I have a recursive scalar function that needs to update a record in another
I have a simple recursive function that calculates a simple pendulum swing decay, using
I have a recursive function that is supposed to print the contents of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.