【问题标题】:get to an UIView frame a zoom animation到 UIView 帧缩放动画
【发布时间】:2012-06-29 12:16:02
【问题描述】:

我正在尝试添加一个带有过渡样式缩放动画的 UIView,如下所示:

     ^
     |
 <---.--->
     |
     v

视图从一个小框架 (centerParent.x, centerParent.y,20, 20) 开始,然后更改为 (centerParent.x, centerParent.y,300, 300)。

但是当我更改框架时,我必须从视图的位置 (0.0) 开始更改它,而不是因为它的中心点。有人知道怎么做吗?

谢谢

【问题讨论】:

  • 你能描述一下你的动画吗?图表对我们来说很难理解。
  • 我试着解释得更好:)
  • 也许你可以将它嵌入到 UIScrollView 中并使用方法 zoomToRect:animated: 来获得相同的效果。

标签: iphone ios ipad uianimation


【解决方案1】:

使用您当前使用的方法(不使用转换,而只是调整视图大小),您只需要重置视图的原点或中心)。最简单的是中心:

 view.frame = CGRectMake(0, 0, 20, 20);
    CGPoint center = view.center;
    [UIView animateWithDuration: 1.0f animations:^{
        view.frame = CGRectMake(0, 0, 300, 300);
        view.center = center;
    }];

您还可以通过移动从 x 和 y 中减去大小变化差的 1/2 来重置原点,但这更多的是数学:

view.frame = CGRectMake(0, 0, 20, 20);
    CGPoint origin = view.frame.origin.
    [UIView animateWithDuration: 1.0f animations:^{
        origin.x -= (300 - 20) / 2;
        origin.y -= (300 - 20) / 2;
        view.frame = CGRectMake(origin.x, origin.y, 300, 300);
    }];

【讨论】:

  • 我使用第二种解决方案。没有重新缩放工作正常!谢谢:)
  • @Aaron Hayman - 请告诉我如何用动画缩小视图。提前致谢。
  • @PankajGupta 您只需要使用我在答案中描述的相同数学。基本上,取原点并减去新尺寸和旧尺寸之间差异的一半。将视图框架设置为动画块内的新原点 + 大小。
【解决方案2】:

要缩小视图,同时保持它自己的中心很容易,但您需要使用变换。这是一个代码。 只需添加并连接一个视图,将其命名为 zoomerView。

在这个例子中,我只是将这个添加到主视图中,触摸结束。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint center = zoomerView.center;
    [UIView animateWithDuration:1 animations:^{
        zoomerView.transform = CGAffineTransformScale(zoomerView.transform, 1.2, 1.2);
    }];
}

希望对你有帮助

【讨论】:

  • 感谢您的回答,但我使用设置框架,因为我需要一个好的分辨率。
【解决方案3】:

试试这个

[UIView animateWithDuration:secs delay:1.0 options:option
                     animations:^{
                         yourview.transform = CGAffineTransformScale(view.transform, 100.0, 100.0);
                     }
                     completion:nil]; 

【讨论】:

  • 这可能行得通,但视图不会不断地重新渲染,所以即使动画有点长,它的质量也会很差。
猜你喜欢
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多