你应该能够得到你使用变换后的效果。
我会做以下事情:
从身份转换开始。
将变换的原点向下移动到图像的底部。
根据需要增加变换的 scale.y。
将变换的原点移回图像的中心
将该变换应用于动画中的按钮。然后将其动画化回身份转换。
如果您不移动变换并仅缩放它,它将从中心向所有方向增长。 (或者如果你只增加 scale.y 则只上下)
编辑:
代码可能如下所示:
let halfHeight = button.bounds.height / 2
//Make the center of the grow animation be the bottom center of the button
var transform = CGAffineTransformMakeTranslation(0, -halfHeight)
//Animate the button to 120% of it's normal height.
tranform = CGAffineTransformScale( transform, 1.0, 1.2)
tranform = CGAffineTransformTranslate( transform, 0, halfHeight)
UIView.animateWithDuration(0.5)
{
button.transform = transform
}
上面的代码将按钮设置为 120% 高度,然后将其保留在那里。
您可以使用 animateWithDuration 的较长变体之一,该变体带有使动画自动反转的选项。我把它留给你作为练习。
编辑#2:
我敲出一些代码来制作一个 3 步动画:
func animateButton(step: Int)
{
let localStep = step - 1
let halfHeight = aButton.bounds.height / 2
var transform: CGAffineTransform
switch step
{
case 2:
//Make the center of the grow animation be the bottom center of the button
transform = CGAffineTransformMakeTranslation(0, -halfHeight)
//Animate the button to 120% of it's normal height.
transform = CGAffineTransformScale( transform, 1.0, 1.2)
transform = CGAffineTransformTranslate( transform, 0, halfHeight)
UIView.animateWithDuration(0.5, animations:
{
aButton.transform = transform
},
completion:
{
(finshed) in
animateButton(step)
})
case 1:
//In the second step, shrink the height down to .25 of normal
transform = CGAffineTransformMakeTranslation(0, -halfHeight)
//Animate the button to 120% of it's normal height.
transform = CGAffineTransformScale( transform, 1.0, 0.25)
transform = CGAffineTransformTranslate( transform, 0, halfHeight)
UIView.animateWithDuration(0.5, animations:
{
aButton.transform = transform
},
completion:
{
(finshed) in
animateButton(step)
})
case 0:
//in the final step, animate the button back to full height.
UIView.animateWithDuration(0.5)
{
aButton.transform = CGAffineTransformIdentity
}
default:
break
}
}
你会调用它
animateButton(3)
使用枚举作为步数会更简洁,它可以使用一些范围检查来确保输入值是 3、2 或 1,但你明白了......