菜鸟教程小白 发表于 2022-12-12 16:59:50

ios - 带有 CGAffineTransformMakeScale 的 UIView 动画立即用十进制数改变大小


                                            <p><pre><code>- (void)startAnimation {
//reverse - shrinking from full size
    if (_reversed == YES) {
      //self.transform = CGAffineTransformMakeScale(1.0, 1.0);
      [UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^{
            self.transform = CGAffineTransformMakeScale(0.1, 0.1); //this line does it instantly
            self.alpha = 0;
      } completion:^(BOOL finished) {
            ;
      }];
    } else {
//non reverse - expanding from middle
      self.transform = CGAffineTransformMakeScale(0.001, 0.001);
      [UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^{
            self.transform = CGAffineTransformMakeScale(1.0, 1.0);
            self.alpha = 0;
      } completion:^(BOOL finished) {
            ;
      }];
    }
}
</code></pre>

<p>非反向代码确实可以正常工作,但是当我执行 _reversed == YES 位时,动画 block 内的转换会立即发生。如果我注释那行代码,则 View 保持正确的大小,但如果我取消注释它,它会立即缩小,但 alpha 仍然会执行淡入淡出动画。为什么会这样?</p>

<p>编辑:我知道发生了什么,但我不知道如何解决它。 View 确实做了动画,只有 View 的大小会立即改变,但它仍然“滑入”中心,好像它正在缩小一样(你看到的只是一个小矩形滑到中间,就好像它是左上角物体)。如果我先将 View 缩放到 2,然后缩小到 1,则动画效果很好,只有当从 1 变为十进制数时它才不起作用。我还使用 draw rect 创建了一个具有核心图形的对象,并且变换问题会影响它,但如果设置了背景颜色,则不会影响实际的框架。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我遇到了类似的问题, View 的位置会在使用 CGAffineTransformMakeScale 进行变换更改动画之前突然跳跃。我注意到“跳跃”的大小似乎与动画后期发生的缩放成正比。</p>

<p>我可以通过在 viewWillLayoutSubviews() 覆盖中发现我正在设置动画 View 的框架来解决此问题。通常,不要设置将具有非身份转换的 View 框架。所以在 viewWillLayoutSubviews() 覆盖中,我设置了 View 的边界和 layer.position,现在动画像丝绸一样平滑。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 带有 CGAffineTransformMakeScale 的 UIView 动画立即用十进制数改变大小,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/20342265/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/20342265/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 带有 CGAffineTransformMakeScale 的 UIView 动画立即用十进制数改变大小