-1

I've got a button. After you click it, it disappears. I want to make it move a UIImageView continuously down a page with one click.

I've tried a for loop statement but the Image just drops down 200 y coordinates. This is the statement:

for (int i = 1; i <= 200; i++)
{
    CGRect oldFrame = _rocketship.frame;
    CGRect newFrame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y + 1, oldFrame.size.width, oldFrame.size.height);
    _rocketship.frame = newFrame;
}
[self.rocketship startAnimating];

I would think that the Image would continuously go down the page, hence the for statement, but it doesn't. Is there any other way to do this?

(PS: The while loop statement does the same thing)

Minestrone-Soup
  • 399
  • 1
  • 16

3 Answers3

1

In IOS the screen is updated when the main thread is available and not currently running anything.

What you are doing is not going to work ever. All you are doing is setting the views position to off the screen because the for loop will finish before the vote is updated.

What you need to do is use core animation.

There is a class method on UIView that starts with "aninateWithDuration". There are several different methods.

These are the methods that you need to use in order to animate anything in IOS.

I can't answer with code at the moment as I'm on a mobile but check the docs for UIView. It will have documentation for these animation methods.

The method is...

[UIView animateWithDuration:1.0
     animations:^{
        // set the end position here
     }];

Also I have a feeling that you will probably have auto layout on. If so let me know because there are different ways of animating if that is the case.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
0

Try something like this instead:

[UIView animateWithDuration:1.0 animations:^{
        CGRect oldFrame = _rocketship.frame;
        CGRect newFrame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y + 200, oldFrame.size.width, oldFrame.size.height);
        _rocketship.frame = newFrame;
}];
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • No, it is not because it is moving incredibly fast or even instantaneously. It is only ever setting the final position. The view is not updated at all during the loop. It gets to the end of the loop before the app then updates the view and takes the current value which then puts it off screen. Like I said in my answer. Also your animation method isn't correct. – Fogmeister Oct 05 '14 at 17:03
  • I understand how that the view isn't updated, that is an extremely simple explanation, it isn't necessary to go incredibly in depth. Like I state above, it isn't accurate as I am on a Windows computer. I will update my answer shortly. – Josue Espinosa Oct 05 '14 at 17:05
  • Yes but with your explanation he might be tempted to put a wait in the loop to "slow it down" but still that wouldn't work. It will just make it take longer for the view to be moved off screen. – Fogmeister Oct 05 '14 at 17:07
  • Good idea. I removed the explanation, so he can look at your answer. – Josue Espinosa Oct 05 '14 at 17:09
  • No need to update your answer Josh, it works perfectly, thanks – Minestrone-Soup Oct 05 '14 at 17:13
  • Lol. So much fail in one stack overflow question. He's basically copied and pasted my answer because his original was so bad. And now you accept that with no explanation. Lol. – Fogmeister Oct 05 '14 at 17:17
0

Josue Espinosa is almost correct

[UIView animationWithDuration:1.0{ CGRect oldFrame = _rocketship.frame; CGRect newFrame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y + 1, oldFrame.size.width, oldFrame.size.height); _rocketship.frame = newFrame;

but you probably want to move the image more then 1 pixel. the way this work is that this will animate the movement to the new location from point A to point B taking the time duration. in the code above it will take 1 second to move the button from point A to point B so say you want to move the button down 200 pixel the coude should be:

[UIView animationWithDuration:1.0 animations:^{ CGRect oldFrame = _rocketship.frame; CGRect newFrame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y + 200, oldFrame.size.width, oldFrame.size.height); _rocketship.frame = newFrame; }];

this will basically animate any visual change you set as being the end result. from state A to state B.

Pascale Beaulac
  • 889
  • 10
  • 28
  • This is also not correct. It's there in the docs (or my answer) if you take a look. – Fogmeister Oct 05 '14 at 17:09
  • yup did not see the missing block segment. From Josue's code. good eyes! I need to get glasses lol. also you can add a completion block after the animation one as like here http://stackoverflow.com/questions/14900916/uiview-animate-and-completion-block – Pascale Beaulac Oct 05 '14 at 17:13