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 9187329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:44:35+00:00 2026-06-17T19:44:35+00:00

I’m trying to make an animation in iOS using the animateWithDuration: method. I’m moving

  • 0

I’m trying to make an animation in iOS using the animateWithDuration: method.

I’m moving an image(a simple picture of a cloud in a UIImageView) across the screen and then having that animation run in a loop and I want to change the speed(duration) each time it crosses the screen.

I’ve tried a couple ways but both behave incorrectly for me.

I first though I could use the UIViewAnimationOptionRepeat like this:

[UIImageView animateWithDuration:arc4random() % 10 + 1
                           delay:0.0
                         options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionRepeat
                      animations:^{
 //moving the cloud across the screen here
}
completion:^(BOOL finished) {
    NSLog(@"Done!");
}];

But this does not seem to call the arc4random() again to reset the duration…i.e. the cloud will cross the screen at a random speed only every time the app is started, not every time the animation loops.

I then tried using the completion block to fire the animation again like this:

-(void)animateMethod
{
[UIImageView animateWithDuration:arc4random() % 10 + 1
                           delay:0.0
                         options:UIViewAnimationOptionCurveLinear
                      animations:^{
 //moving the cloud across the screen here
}
completion:^(BOOL finished) {
    NSLog(@"Done!");
    [self animateMethod];
}];
}

This gives me the effect I’m looking for, but when I push to another view using a navigation controller the completion block gets fired in an unending loop it seems(my log gets spammed with “Done!”)

Anyone know the way to get the desired effect I want the proper way?

  • 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-06-17T19:44:36+00:00Added an answer on June 17, 2026 at 7:44 pm

    You’re on the right track. The key is that you need to only loop if the animation finished not if it failed. So you need to check if the finished BOOL is true before telling it to loop.

    -(void)animateMethod
    {
        [UIImageView animateWithDuration:arc4random() % 10 + 1
                                   delay:0.0
                                 options:UIViewAnimationOptionCurveLinear
                              animations:^{
         //moving the cloud across the screen here
        }
        completion:^(BOOL finished) {
            if (finished) {
                NSLog(@"Done!");
                [self animateMethod];
            }
        }];
    }
    

    This method works great for very simple animations like this and when you’re only doing a few, like maybe 3-5 clouds at a time. Anything more than this and you’ll probably want to setup your own animation loop using an NSTimer or CADisplayLink and adjusting the clouds frames in it. Its a much more manual way but it will get you some nice animations even in UIKit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am using JSon response to parse title,date content and thumbnail images and place

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.