转换应用时Imageview改变位置?

时间:2014-01-20 12:39:34

标签: ios iphone ipad uiimageview uiviewanimation

我有一个iPhone应用程序,我想在不改变图像视图位置的情况下将图像视图转换为双倍大小。(即中心无需更改)我这样做

 CGAffineTransform firstTransform = bg.transform;

 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration: 0.5];
 bg.transform = CGAffineTransformScale(bg.transform,2,2);
 [UIView setAnimationDelegate:self];
 [UIView commitAnimations];
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration: 0.5];
 bg.transform = firstTransform;
 [UIView setAnimationDelegate:self];

 [UIView commitAnimations];

但是当改变它的位置时会发生变化 现在我这样做是为了不改变图像视图的位置。我希望图像视图在点击时转换为双倍尺寸,并带有这样的动画

animation on the red button when clicking next

任何人都可以指导我吗?

2 个答案:

答案 0 :(得分:0)

确保将锚点设置为中心,在设置锚点时,请注意该值必须介于0和1:0之间,左/上侧为1,右侧/下侧为1。因此,要将锚点设置为中间,请在X轴和Y轴上将其设置为0.5:

[itemName.layer setAnchorPoint:CGPointMake(0.5, 0.5)];

然后,当您缩放对象时,它应该从中心点周围向上扩展。

我还建议使用animateWithDuration:completion。代码将变得更加简单:

[UIView animateWithDuration:1.0f animations:^{
    [bg setTransform:CGAffineTransformScale(bg.transform, 2.0, 2.0)];
} completion ^{
    [UIView animateWithDuration:1.0f animations:^{
        [bg setTransform:CGAffineTransformScale(bg.transform, 0.5, 0.5)];
    ];
}];

希望这可以帮助您达到预期的效果!

答案 1 :(得分:0)

确保您没有使用自动布局。要禁用打开xib,请单击右侧面板中的第一个选项卡,取消选中Interface Builder Document下的Use Auto Layout。附上屏幕截图以寻求帮助。

Auto Layout under Interface Builder Document