calm / KenBurns

A little Swift utility that performs the Ken Burns effect on an image
http://calm.com/jobs
MIT License
31 stars 18 forks source link

Animate right away #3

Closed thbaja closed 7 years ago

thbaja commented 7 years ago

Is there an easy way to get the image shown and start animating right away? It seems it needs to complete one cycle first, which will last for an interval within the durationRange.

A working dirty hack is doing something like this:

private var firstRun = true
func startNewAnimation() {
    var dur = durationRange
    if firstRun {
        firstRun = false
        dur = DurationRange(0, max: 0.1)
    }
    currentImageView.transform = CGAffineTransform.identity
    currentImageView.size = self.size   
    let animation = KenBurnsAnimation(targetImage: currentImageView, zoomIntensity: zoomIntensity, durationRange: dur, pansAcross: pansAcross)
    animation.completion = self.didFinishAnimation
    animation.willFadeOut = self.willFadeOutAnimation
    animations.append(animation)
}
tsheaff commented 7 years ago

@thbaja you should just need to call setImage before startAnimating right? That sets the image immediately.

thbaja commented 7 years ago

Sorry for not getting back to you on this issue before, but now I've had a chance to take a look on it.

I'm using autolayout to set up the UI including the KenBurnsImageView. I do this on viewDidLoad, which means the subviews haven't been laid out yet giving a size of zero. When starting a new animation cycle the size of self is used, which can be seen in the code snippet above. That is why I had to wait an animation cycle before seeing the image and animation.

For future reference I think the best solutions in prioritized order are:

  1. Don't use autolayout.
  2. Alternative is to start animation in viewDidLayoutSubviews, but one needs to be aware that this can be called several times.
  3. Starting animations in viewDidAppear is a bit to late and doesn't look good.
tsheaff commented 7 years ago

Thanks for the thorough discussion @thbaja