ios - 动画 UIButton 以在单击时放大和缩小

标签 ios swift xcode uibutton

我有一个按钮,我已将其替换为一个图标,当单击该图标时,我希望它放大和缩小 5 秒。我怎样才能做到这一点?我为按钮制作了一组 5 张不同尺寸的图片,我可以循环使用这些图片还是有其他方法?

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

编辑:我正在使用 Xcode 6.4

最佳答案

为了展示替代方案,我将展示一种使用动画图层的方法。 更多相关信息 here

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

// 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)

结果:

enter image description here

关于ios - 动画 UIButton 以在单击时放大和缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38951272/

相关文章:

ios - UIPageViewController 错误中的导航栏

ios - 使用变量设置 UISlider 的值

ios - 在现有的 xcode 项目上使用 swift 包管理器

objective-c - 如何在 XCode 中将自定义 UIButton 添加到 nib 文件?

ios - 生成错误 : "Unknown type name ' ffi_cif'"

iOS - watchOS App 发布问题 CFBundleIdentifier 冲突

objective-c - 使用Cocoa Touch在运行时创建控件

ios - 将值传递给 WKWebView Swift

ios - 如何在 iOS 和 tvOS 上使用相同的 pod?

ios - 不显示导航栏