正确使用CGAffineTransformMakeScale

Sid*_*rth 3 core-animation objective-c cgaffinetransform uiviewanimation ios

我有一个UIButton使用故事板的布局.该按钮只包含一个图像.单击该按钮时,我想设置按钮大小的动画 - 减小尺寸,然后再将其恢复为原始大小.

我用了以下代码 -

[UIView animateWithDuration:2.0 animations:^{
    _favButton.transform = CGAffineTransformMakeScale(0.5, 0.5);
}completion:^(BOOL finished) {
    [UIView animateWithDuration:2.0 animations:^{
          _favButton.transform = CGAffineTransformMakeScale(1, 1);
    }];
}];
Run Code Online (Sandbox Code Playgroud)

这段代码移动了我不想要的屏幕上的按钮.我希望center修复按钮并调整大小.

我没有Top Constraint在故事板中使用任何按钮.我该如何纠正这种行为?

Unh*_*lig 8

如果您打开了自动布局,则需要将其关闭.

但根据您的描述,这似乎不是您的问题.

我会做以下重新调整到中心,因为它缩放:

CGPoint cP = _favButton.center;

[UIView animateWithDuration:2.0 animations:^
{
    _favButton.transform = CGAffineTransformMakeScale(0.5, 0.5);
    _favButton.layer.position = cp;
}
completion:^(BOOL finished) 
{
    [UIView animateWithDuration:2.0 animations:^
    {
        _favButton.transform = CGAffineTransformMakeScale(1, 1);
        _favButton.layer.position = cp;
    }];
}];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.