首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动画化UIButton以放大和缩小单击

问动画化UIButton以放大和缩小单击
EN

Stack Overflow用户
提问于 2016-08-15 07:53:27
回答 2查看 2.7K关注 0票数 0

我有一个按钮,我已经用一个图标取代,当图标是陈词滥调,我希望它放大和缩小,让我们说,5秒。我怎样才能做到这一点?我已经为按钮做了一套不同大小的5张图片,我可以循环这些图片,还是有其他的方法?

代码语言:javascript
复制
@IBAction func myButton(sender: UIButton){
    //animation that zoom the button icon in and out
}

编辑:我使用Xcode 6.4

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-15 07:59:55

这将放大和缩小按钮,而不使用其他图像:

代码语言:javascript
复制
let timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "stopButtonAnimation", userInfo: nil, repeats: false)
let options = UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(0.25, delay: 0, options: options, 
animations: {
    self.button.transform = CGAffineTransformMakeScale(0.5, 0.5)
}, completion:nil)

.

代码语言:javascript
复制
func stopButtonAnimation() {
    button.layer.removeAllAnimations;
}
票数 2
EN

Stack Overflow用户

发布于 2016-08-15 16:18:31

为了展示另一种选择,我将展示一种具有动画层的方法。更多关于it的信息这里

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

代码语言:javascript
复制
// 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)

结果:

​

​

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38951272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档