Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Michael Doyle
Michael Doyle
2,418 Points

Need to pause and restart execution in loop

I want to move an icon around on the iPhone screen. I made an array of positions and one of ypositions. I get a random number and return a position from a function as xPosition, yPosition.

I figured I can change the x and y position of the UIImage in a loop. If I were to execute the loop, I'm sure the computer would run so fast I would only actually see the last position. I need to pause for a second before changing the x and y position of the icon so it appears to jump around on the screen. I'm not sure how to do that.

If there is a better way to do what I am trying to do, I would certainly like to know. Thanks

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

The fast way to accomplish this task would be to use the C sleep function, but the likely safer and more object-oriented way to do this would be to use NSTimer