Search code examples
iosswiftanimationuiviewanimationuiviewanimationtransition

Swift Zoom Transition Animation is not working in only second time


I created custom transition, NSZoomTransitionAnimator.

I have a problem that the transition is not working when goingBack in only second time.

For example, I touched a cell (indexPath.row = 0) in collection view. Working correctly in first time , but not working in second time.

I debug code and found sourceImageView.frame is (0.0, 0.0, 360.0, 480.0) in second time.

(0.0, 0.0, 360.0, 480.0) is correct.

Here is my code,NSZoomTransitionAnimator.swift.

UIView.animateWithDuration(kBackwardAnimationDuration, delay: 0, options: .CurveEaseOut, animations: {
  sourceImageView.frame = destinationImageViewFrame
  println(sourceImageView.frame)//(0.0, 64.0, 187.5, 187.5)

}, completion: {(finished: Bool) in
  if finished {
      destinationImageView.removeFromSuperview()

      println(sourceImageView.frame)//(0.0, 0.0, 360.0, 480.0)
      sourceImageView.removeFromSuperview()
  }
  transitionContext.completeTransition(finished)
})

Here is my repository.
https://github.com/naoyashiga/NSZoomTransitionAnimator

Touch a image in collection view and touch a close button in DetailViewController. So transition will start.

please tell me why transition is not working in only second time.


Solution

  • You can't re-use the sourceViewController.transitionSourceImageView() which has two constraints:

    (lldb) po sourceImageView.constraints()
    [
    <NSContentSizeLayoutConstraint:0x7feaeb7097a0 H:[UIImageView:0x7feaeb577230(360)] Hug:251 CompressionResistance:750>, 
    <NSContentSizeLayoutConstraint:0x7feaeb704680 V:[UIImageView:0x7feaeb577230(480)] Hug:251 CompressionResistance:750>
    ]
    

    So its size will be incorrect. Instead, you can change

    sourceImageView = sourceViewController.transitionSourceImageView()
    

    to

    sourceImageView.image = sourceViewController.transitionSourceImageView().image
    sourceImageView.frame = sourceViewController.transitionSourceImageView().frame
    

    This will solve your problem.