动画UIButton可以放大和缩小点击

The*_*ude 0 xcode uibutton ios swift

我有一个按钮,我用一个图标替换,当图标是陈词滥调时,我希望它放大和缩小让我们说5秒钟.我怎么能做到这一点?我为按钮制作了一组5个不同大小的图像,我可以通过theese循环还是有另一种方式?

@IBAction func myButton(sender: UIButton){
    //animation that zoom the button icon in and out
}
Run Code Online (Sandbox Code Playgroud)

编辑:我正在使用Xcode 6.4

ron*_*ory 7

为了显示替代方案,我将展示一种动画层的方法.关于它的更多信息在这里

将此代码添加到您的函数中(提示在代码注释中):

// specify the property you want to animate
let zoomInAndOut = CABasicAnimation(keyPath: "transform.scale")
// starting from the initial value 1.0
zoomInAndOut.fromValue = 1.0
// to scale down you set toValue to 0.5
zoomInAndOut.toValue = 0.5
// the duration for the animation is set to 1 second
zoomInAndOut.duration = 1.0
// how many times you want to repeat the animation
zoomInAndOut.repeatCount = 5
// to make the one animation(zooming in from 1.0 to 0.5) reverse to two animations(zooming back from 0.5 to 1.0)
zoomInAndOut.autoreverses = true
// because the animation consists of 2 steps, caused by autoreverses, you set the speed to 2.0, so that the total duration until the animation stops is 5 seconds
zoomInAndOut.speed = 2.0
// add the animation to your button
button.layer.addAnimation(zoomInAndOut, forKey: nil)
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述