-3

I have a problem with animation as I want the same animation with different end points, to be called upon finishing the previous animation and to be executed smoothly without slight stop.

Let me elaborate with a sample code and comments.

Func animate()
{
Start animation(completionHandler: {

If condition is true 

Repeat the animation and check after it finishes

}

}

but the problem with this code is that, between finishing the first animation and starting another one, there will be a split second stop which is not smooth movement. For example, I have a view on zero point to move with animation to point A (x:100,y:100), and then it will receive point B (x:500, y:900), after it reaches point A it will stop a split second then move to point B. Since the points are Dynamic, I cannot know if there is a new point or not, and if I did i will not know the coordinates beforehand, so I need to keep the logic dynamic and keeps the animation smooth between points.

I tried many logics and results were not the thing I wanted. problems I faced toward a smooth queued animation are:

  1. I tried UIView.animate in the following:

    a. I tried setting an animation duration let's say 2 sec, then after it finishes, it calls animation again within completion hander/closure/block, but it will result in a less than second stop between the two animations.

    b. I tried to start 2 animations, the second is delayed with the exact duration go the first animation, but results also brief stop between 2 animations.

2.I tried animation with keyframes, but since I don't know all animations in advance, this won't work.

Can you help please with my approach toward a smooth dynamic animation?

Masashi
  • 350
  • 4
  • 15
  • Can you post your code that you have for your animation currently? – Jake Apr 15 '18 at 10:47
  • This answer for Objective-C but you can do similar with Swift:https://stackoverflow.com/questions/14730460/how-to-make-uiview-animation-sequence-repeat-and-autoreverse – Raviprakash Apr 15 '18 at 10:48
  • @Jake Well I can, but mainly its a calling for UIview.animate in a for loop which is not sufficient – Masashi Apr 15 '18 at 10:49
  • @Raviprakash problem is I don’t know how many times i need to animate, it depends on a conditions so it’s not a fixed number of times – Masashi Apr 15 '18 at 10:51
  • Can anyone at least tell me what the down vote for? Please enlighten me – Masashi Apr 15 '18 at 11:27
  • You’ve answered your own question. Just do exactly what your pseudocode says. Recurse. – matt Apr 15 '18 at 11:51

1 Answers1

1

Your own pseudocode answers your question. Just test whatever your condition, and if you need to animate again, work out the new start and end points and animate again:

func animate(start:Int, end:Int) {
    UIView.animate(withDuration: 5, animations: {
        // animate here based on new start and end
    }, completion: { _ in
        var weShouldDoItAgain = false
        // figure out if we should do it again
        // if so, change weShouldDoItAgain to true
        if weShouldDoItAgain {
            var start = start // and now change it to new value
            var end = end // and now change it to new value
            self.animate(start:start, end:end)
        }
    })
}

Just change all the values and types to suit your use case.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I implemented something similar and worked fine, although I hoped for a cleaner and dependent solution, and closures don’t fully help. – Masashi Apr 16 '18 at 04:25
  • I don’t know what cleaner and dependent and closures mean here. – matt Apr 16 '18 at 04:50
  • I meant that instead of checking in after animation block, I wanted a more generalized method, but seems this is the only way to do it. – Masashi Apr 16 '18 at 04:52
  • i have a problem now that completion handler is called at same time of animations, not after animations complete, why? – Masashi Apr 16 '18 at 08:10
  • How do I know? You never showed any real code. I have no idea what you’re doing. Please do not leech in the comments. If you have a new question, click Ask Question and ask it. – matt Apr 16 '18 at 12:26
  • But since im asking about same solution, i think I’ve done nothing wrong as its not a new question – Masashi Apr 16 '18 at 12:37
  • If my answer is right, the exchange is over. If my answer is wrong, unaccept it. – matt Apr 16 '18 at 12:47