【问题标题】:Custom UIView scaling using CGAffineTransformScale使用 CGAffineTransformScale 自定义 UIView 缩放
【发布时间】:2014-03-01 22:25:24
【问题描述】:

我正在学习为 iOS 开发,作为教育过程的一部分,我创建了自定义 UIView,它使用 drawRect 绘制其内容(基于核心图形 + UIBezierPath)。一切都按预期工作,视图是动态的,并根据其宽度/高度呈现其内容。现在我想添加溶解动画,看起来应该从 100% 缩放到 0%。我已经写了这段代码来做到这一点:

[UIView animateWithDuration:1 delay:0
                options: UIViewAnimationOptionBeginFromCurrentState
             animations: ^{
     for (CardView *cardView in self.cardsViews) {
         cardView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.01, 0.01);

     }} completion:nil];

但是,此代码不会将我的视图从 100% 缩放到 0%。它只会让卡片消失(最多)。我尝试了很多缩放方法,但到目前为止我达到的唯一效果是从 0% 缩放到 100%。这是我的观点完美,但不是反向缩放......此外,即使我尝试应用非动画转换,它也不会放大/缩小,例如:

cardView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.5, 0.5);

我看到我的视图外观发生了一点变化,但没有缩放到 50%,这是肯定的。当我尝试将完全相同的动画逻辑应用于 UILabel 时,它可以完美运行!开发自定义 UIView 我错过了什么?为什么缩放视图可能会出现故障? 提前非常感谢!

更新 #1 此代码使我的视图正好大两倍,然后将其缩放回原始大小:

[UIView animateWithDuration:5 delay:0
                    options: UIViewAnimationOptionBeginFromCurrentState
                 animations: ^{
         for (CardView *cardView in self.cardsViews) {
             cardView.transform = CGAffineTransformMakeScale(0.5, 0.5);
         }
    } completion:nil];

【问题讨论】:

  • 您是否尝试过添加动画选项UIViewAnimationOptionAllowAnimatedContent
  • 谢谢。是的,我试过了。没运气。此外,即使没有动画,我的自定义视图也不会缩放...
  • @Serzhas 你用这个有什么收获吗?
  • 是的,我记得我以某种方式解决了这个问题,但那是很久以前的事了,我不再从事那个项目了,所以我无法检查代码......抱歉。 ??????

标签: ios uiview transformation


【解决方案1】:

使用 CGAffineTransformScale 很简单 100% 到 0%:

cardView.view.transform =CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);

[UIView animateWithDuration:0.3/1.5 animations:^{
        cardView.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    } completion:^(BOOL finished)
    {
              NSLog(@" Animation complet Block");
    }];

【讨论】:

    【解决方案2】:

    您可以尝试将视图的contentMode 设置为UIViewContentModeRedraw 并在animateWithDuration 方法中添加另一个动画选项 - UIViewAnimationOptionAllowAnimatedContent

    【讨论】:

    • 谢谢!我会尝试并报告。
    • 不,不幸的是,这无济于事......我的视图已经有适当的 contentMode,添加动画选项没有任何区别。请注意,即使没有动画,我的视图也不会缩放。只是试图使它成为原始的 50% 不会做任何事情......:/
    【解决方案3】:

    从 120% 快速缩放到 100%:

    logoView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
    UIView.animate(withDuration: 0.55, delay: 0.1, options: [.curveEaseOut], animations: {
        self.logoView.transform = .identity
        self.view.layoutIfNeeded()
    }, completion: { _ in
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多