I'm having trouble with unexpected movements with animation using Timer.

Asked 2 years ago, Updated 2 years ago, 34 views

After generating a toyImageCustomView every 4 seconds using a timer and moving it out of the screen,
I'd like to do an animation to erase the toyImageCustomView.

For some reason, as soon as the first toyImageCustomView disappears off the screen, the last toyImageCustomView is also
It disappears even in the middle of the screen.

I just don't know why.

Could someone please teach me?
Thank you for your cooperation.

Code to run the timer.
Animation driven by a timer.

swift

2022-09-30 19:41

1 Answers

In line 275, the instance variable toyImageCustomView is overwritten every time this method is called by a timer, so the toyImageCustomView is the last view that you are moving in order of animation.
(One animation is called every 4 seconds, so it's the 7th or 8th view.)

And since the animation of the first view ends and the completion handler deletes the instance variable toyImageCustomView, it will disappear in the last view, not the first one.

For now, if you want to fix it simply, instead of using toyImageCustomView as an instance variable, you can use it as a local variable, capture it in the closure every time, and call the method for the captured variable.


2022-09-30 19:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.