菜鸟教程小白 发表于 2022-12-11 20:07:14

ios - Objective-c 动画与尾随约束的约束不稳定问题


                                            <p><p>我正在使用 Objective-c 开发一个 IOS 应用程序。在为 View 的约束设置动画时,我遇到了一个不稳定的问题。动画在帧的最终位置上效果很好,但随机地,第一个动画 block 省略了尾随(右)约束(左约束永远不会达到 -20 常数值),但是前导(​​左)、顶部和底部约束正在按预期工作。</p>

<p>请看我上传的视频。在第一次点击中,动画按预期工作,但在第二次点击中出现问题。请指教。</p>

<p> <a href="https://youtu.be/Smh-_8urBlA" rel="noreferrer noopener nofollow">Video showing the problem</a> </p>

<pre><code>- (void)animateTransition:(nonnull id&lt;UIViewControllerContextTransitioning&gt;)transitionContext {
// 1
UIView *containerView = transitionContext.containerView;
UIViewController* toViewController = ;

// 2
;

if(]){
    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);
    ;

    NSTimeInterval duration = ;
    ;
    [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.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.configureRoundedCorners(shouldRound: false)
      } completion:^(BOOL finished) {
            ;
      }];
    }];
}
</code></pre>

<p>}</p>

<p>@Sh_Khan 发表评论后更新代码。</p>

<pre><code>- (void)animateTransition:(nonnull id&lt;UIViewControllerContextTransitioning&gt;)transitionContext {
// 1
UIView *containerView = transitionContext.containerView;
UIViewController* toViewController = ;

// 2
;
if(]){
    ImageViewController *vcImage = (ImageViewController*)toViewController;
    NSTimeInterval duration = ;

    [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;

      ;
    } 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.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.configureRoundedCorners(shouldRound: false)
            } completion:^(BOOL finished) {
                ;
            }];
      }];
    }];
}
</code></pre>

<p>}</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在设置您的 imageView 之前,请尝试重新定位照片。有些东西告诉我 ImageView 很难确定图像的方向。我曾经在过渡期间发生过这种情况,并会解释为什么您只在某些时候看到这种行为。我对 Objective C 有点生疏,但在设置 imageView 图像之前试一试并使用它。</p>

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

    UIGraphicsBeginImageContextWithOptions(currentImage.size, NO, currentImage.scale);
    ;
    UIImage *_newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return _newImage;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Objective-c 动画与尾随约束的约束不稳定问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50162812/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50162812/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Objective-c 动画与尾随约束的约束不稳定问题