2

I'm developing an IOS app using objective-c. I came across with an instability problem when animating a view's constraints. The animation works well regarding the final position of the frame, but randomly, the trailing(right) constraint is omitted for the first animation block(left constraint never reach to -20 constant value), however leading(left), top and bottom constraints are working as expected.

Please see the video I uploaded. In the first tap, the animation is working as expected, but in the second tap the issue occurs. Please advice.

Video showing the problem

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// 1
UIView *containerView = transitionContext.containerView;
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

// 2
[containerView addSubview:toViewController.view];

if([toViewController isKindOfClass:[ImageViewController class]]){
    ImageViewController *vcImage = (ImageViewController*)toViewController;
    vcImage.constraintLeft.constant = 20;
    vcImage.constraintTop.constant = self.selectedCardFrame.origin.y + 60.0;
    vcImage.constraintRight.constant = 20.0;
    vcImage.constraintBottom.constant = 0;//self.CollectionView.bounds.size.height - (self.selectedCardFrame.origin.y + self.selectedCardFrame.size.height);
    [toViewController.view layoutIfNeeded];

    NSTimeInterval duration = [self transitionDuration:transitionContext];
    [toViewController.view layoutIfNeeded];
    [UIView animateWithDuration:duration animations:^{
        //First animation block
        vcImage.constraintRight.constant = -20.0;
        vcImage.constraintLeft.constant = -20.0;
        vcImage.constraintTop.constant = -20.0;
        vcImage.constraintBottom.constant = 0;
        [toViewController.view layoutIfNeeded];
        //toViewController.configureRoundedCorners(shouldRound: false)
    } completion:^(BOOL finished) {
        //Second animation block
        [UIView animateWithDuration:duration animations:^{
            vcImage.constraintLeft.constant = 0;
            vcImage.constraintTop.constant = 0.0;
            vcImage.constraintRight.constant = 0;
            vcImage.constraintBottom.constant = 0;
            [toViewController.view layoutIfNeeded];
            //toViewController.configureRoundedCorners(shouldRound: false)
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:finished];
        }];
    }];
}

}

Updated code after @Sh_Khan 's comment.

- (void)animateTransition:(nonnull id<UIViewControllerContextTransitioning>)transitionContext {
// 1
UIView *containerView = transitionContext.containerView;
UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

// 2
[containerView addSubview:toViewController.view];
if([toViewController isKindOfClass:[ImageViewController class]]){
    ImageViewController *vcImage = (ImageViewController*)toViewController;
    NSTimeInterval duration = [self transitionDuration:transitionContext];

    [UIView animateWithDuration:0 animations:^{
        vcImage.constraintLeft.constant = 20;
        vcImage.constraintTop.constant = self.selectedCardFrame.origin.y + 60.0;
        vcImage.constraintRight.constant = 20.0;
        vcImage.constraintBottom.constant = 0;

        [toViewController.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:duration animations:^{
            //First animation block
            vcImage.constraintRight.constant = -20.0;
            vcImage.constraintLeft.constant = -20.0;
            vcImage.constraintTop.constant = -20.0;
            vcImage.constraintBottom.constant = 0;
            [toViewController.view layoutIfNeeded];
            //toViewController.configureRoundedCorners(shouldRound: false)
        } completion:^(BOOL finished) {
            //Second animation block
            [UIView animateWithDuration:duration animations:^{
                vcImage.constraintLeft.constant = 0;
                vcImage.constraintTop.constant = 0.0;
                vcImage.constraintRight.constant = 0;
                vcImage.constraintBottom.constant = 0;
                [toViewController.view layoutIfNeeded];
                //toViewController.configureRoundedCorners(shouldRound: false)
            } completion:^(BOOL finished) {
                [transitionContext completeTransition:finished];
            }];
        }];
    }];
}

}

Semih Akbas
  • 43
  • 1
  • 5

1 Answers1

0

Before setting your imageView try reorienting the photo. Something tells me the image view is having a hard time determining the orientation of the image. I had this happen during a transition once and would explain why you are seeing the behavior only some of the time. I am a bit rusty with Objective C but give this a shot and use it to before setting your imageView image.

-(UIImage*)normalizeImage:(UIImage*)currentImage {
    if (currentImage.imageOrientation == UIImageOrientationUp){
        return currentImage;
    }

    UIGraphicsBeginImageContextWithOptions(currentImage.size, NO, currentImage.scale);
    [currentImage drawInRect:CGRectMake(0, 0, currentImage.size.width, currentImage.size.height)];
    UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return _newImage;
}
agibson007
  • 4,173
  • 2
  • 19
  • 24
  • This solves the problem, however the previous answers are also solution but since I create the Imageview during runtime, it did not solved my problem. Thanks for your help, really appreciated. – Semih Akbas May 05 '18 at 11:01