3

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

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

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

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

谢谢

4

3 回答 3

7

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

 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);
    }];
于 2012-06-29T13:01:15.380 回答
1

要缩小视图,同时保持它自己的中心很容易,但您需要使用变换。这是一个代码。只需添加并连接一个视图,将其命名为 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);
    }];
}

希望这可以帮助你

于 2012-06-29T12:37:24.073 回答
0

试试这个

[UIView animateWithDuration:secs delay:1.0 options:option
                     animations:^{
                         yourview.transform = CGAffineTransformScale(view.transform, 100.0, 100.0);
                     }
                     completion:nil]; 
于 2012-06-29T12:40:21.387 回答