Ios 带约束的Objective-c动画尾部约束的不稳定性问题

Ios 带约束的Objective-c动画尾部约束的不稳定性问题,ios,objective-c,uiviewcontroller,ios-animations,Ios,Objective C,Uiviewcontroller,Ios Animations,我正在用objective-c开发一个IOS应用程序。在设置视图约束的动画时,我遇到了一个不稳定的问题。关于帧的最终位置,动画效果良好,但随机地,第一个动画块的尾部(右)约束被忽略(左约束永远不会达到-20常量值),但是前导(左)、顶部和底部约束按预期工作 请看我上传的视频。在第一次轻触中,动画按预期工作,但在第二次轻触中出现问题。请给我一些建议 -(void)animateTransfion:(非空id)transitionContext{ // 1 UIView*containerView

我正在用objective-c开发一个IOS应用程序。在设置视图约束的动画时,我遇到了一个不稳定的问题。关于帧的最终位置,动画效果良好,但随机地,第一个动画块的尾部(右)约束被忽略(左约束永远不会达到-20常量值),但是前导(左)、顶部和底部约束按预期工作

请看我上传的视频。在第一次轻触中,动画按预期工作,但在第二次轻触中出现问题。请给我一些建议

-(void)animateTransfion:(非空id)transitionContext{
// 1
UIView*containerView=transitionContext.containerView;
UIViewController*toViewController=[transitionContext ViewControllerWorky:UITransitionContextToViewControllerKey];
// 2
[containerView添加子视图:toViewController.view];
如果([toViewController是类的种类:[ImageViewController类]]){
ImageViewController*vcImage=(ImageViewController*)到ViewController;
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 LAYOUTIFNEED];
NSTimeInterval持续时间=[自转换持续时间:transitionContext];
[toViewController.view LAYOUTIFNEED];
[UIView animateWithDuration:持续时间动画:^{
//第一个动画块
vcImage.constraintRight.constant=-20.0;
vcImage.constraintLeft.constant=-20.0;
vcImage.constraintTop.constant=-20.0;
vcImage.constraintBottom.constant=0;
[toViewController.view LAYOUTIFNEED];
//toViewController.configureRoundedCorners(shouldRound:false)
}完成:^(布尔完成){
//第二动画块
[UIView animateWithDuration:持续时间动画:^{
vcImage.constraintLeft.constant=0;
vcImage.constraintTop.constant=0.0;
vcImage.constraintRight.constant=0;
vcImage.constraintBottom.constant=0;
[toViewController.view LAYOUTIFNEED];
//toViewController.configureRoundedCorners(shouldRound:false)
}完成:^(布尔完成){
[transitionContext completeTransition:完成];
}];
}];
}
}

更新了@Sh_Khan评论后的代码

- (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];
            }];
        }];
    }];
}
-(void)animateTransfion:(非空id)transitionContext{
// 1
UIView*containerView=transitionContext.containerView;
UIViewController*toViewController=[transitionContext ViewControllerWorky:UITransitionContextToViewControllerKey];
// 2
[containerView添加子视图:toViewController.view];
如果([toViewController是类的种类:[ImageViewController类]]){
ImageViewController*vcImage=(ImageViewController*)到ViewController;
NSTimeInterval持续时间=[自转换持续时间:transitionContext];
[UIView animateWithDuration:0动画:^{
vcImage.constraintLeft.constant=20;
vcImage.constraintTop.constant=self.selectedCardFrame.origin.y+60.0;
vcImage.constraintRight.constant=20.0;
vcImage.constraintBottom.constant=0;
[toViewController.view LAYOUTIFNEED];
}完成:^(布尔完成){
[UIView animateWithDuration:持续时间动画:^{
//第一个动画块
vcImage.constraintRight.constant=-20.0;
vcImage.constraintLeft.constant=-20.0;
vcImage.constraintTop.constant=-20.0;
vcImage.constraintBottom.constant=0;
[toViewController.view LAYOUTIFNEED];
//toViewController.configureRoundedCorners(shouldRound:false)
}完成:^(布尔完成){
//第二动画块
[UIView animateWithDuration:持续时间动画:^{
vcImage.constraintLeft.constant=0;
vcImage.constraintTop.constant=0.0;
vcImage.constraintRight.constant=0;
vcImage.constraintBottom.constant=0;
[toViewController.view LAYOUTIFNEED];
//toViewController.configureRoundedCorners(shouldRound:false)
}完成:^(布尔完成){
[transitionContext completeTransition:完成];
}];
}];
}];
}

}

在设置图像视图之前,请尝试重新确定照片的方向。有些东西告诉我图像视图很难确定图像的方向。我曾经在一次转换中遇到过这种情况,我会解释为什么你只在某些时候看到这种行为。我对Objective C有点生疏,但在设置imageView图像之前,先给它拍一张照片,然后用它来显示

-(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;
}

视野里是什么?只是一个图像视图?toViewController?它只是一个带有UIImageView的控制器吗?@agibson007在滚动视图中有一个滚动视图和一个图像视图。但它并不总是这样做?是的,这就是问题所在。您可以在主要帖子中看到视频中的操作问题这解决了问题,但是前面的答案也是解决方案,但是因为我在运行时创建了Imageview,所以没有解决我的问题。谢谢你的帮助,非常感谢。
-(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;
}