【问题标题】:Scale/Animate One Side of UIView to Be Smaller? - CGAffineTransformMakeScale缩放/动画 UIView 的一侧变小? - CGAffineTransformMakeScale
【发布时间】:2014-07-08 01:41:15
【问题描述】:

我想为 UIView 设置动画,使其右侧变小,然后再返回。当点击此 UIView 中的 UIButton 时,将触发此动画。这是我想要的快速模型 - UIView 将从状态 1 > 状态 2 > 状态 1:

它看起来像是被推到一侧。

这是我的另一个动作的代码 - 这使得 UIView 变得越来越小,就好像它被推到中心而不是侧面一样。

self.myView.transform = CGAffineTransformMakeScale(0.95,0.95);
self.myView.alpha = 1.f;

[UIView beginAnimations:@"button" context:nil];
[UIView setAnimationDuration:0.5];
self.myView.transform = CGAffineTransformMakeScale(1,1);
self.myView.alpha = 1.0f;
[UIView commitAnimations];

如何将相同的效果应用到右侧?任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 这是一个透视变换,不能用仿射变换完成。您需要绕 Y 轴旋转,而这种类型的矩阵无法完成。
  • 我该如何改变它才能产生这种效果?
  • 您可以尝试将视图转换为图像并对其应用CIPerspectiveTransform 过滤器。
  • @user1118321 哦,我确实想转换一个显示 UIImage 的 UIImageView(分别称为 myImageView 和 myImage)。它们只是在 UIView 中。

标签: ios cocoa-touch core-animation uiviewanimation cgaffinetransform


【解决方案1】:

根据这个question,例如,你需要进行透视变换,我从我链接的那个问题中修改了一点

  CATransform3D perspectiveTransform = CATransform3DIdentity;
  perspectiveTransform.m34 = 1.0 / -500;
  perspectiveTransform = CATransform3DRotate(perspectiveTransform, 40.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);

  [UIView animateWithDuration:0.4 animations:^{
    self.myView.layer.transform = perspectiveTransform;

  }];

EDIT 2 恢复原状

 - (void)startAnimation:(id)sender
  {
     CATransform3D perspectiveTransform = CATransform3DIdentity;
     perspectiveTransform.m34 = 1.0 / -500;
     perspectiveTransform = CATransform3DRotate(perspectiveTransform, 40.0f * M_PI / 180.0f, 0.0f, 1.0f, 0.0f);
     [UIView animateWithDuration:1.4 animations:^{
     self.myView.layer.transform = perspectiveTransform;
     }completion:^(BOOL finished) {

      CATransform3D originalPerspectiveTransform = CATransform3DIdentity;
      [UIView animateWithDuration:0.9 animations:^{
         self.myView.layer.transform = originalPerspectiveTransform;
        }];
     }];

  }

【讨论】:

  • 非常感谢,它成功了!还有一件事——我怎样才能让它回到原来的位置?就像我在原始问题中发布的代码一样,当我点击中心的按钮时,它会恢复到原始大小/位置。在这种情况下,我如何让它回到原来的视角(请看我在 OP 中发布的草图)。我不知道该怎么做:S
  • 非常感谢珊 :) 非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多