如何在 UIButton 上制作原生“脉冲效果”动画 - iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8083138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:19:40  来源:igfitidea点击:

How to do a native "Pulse effect" animation on a UIButton - iOS

iosobjective-canimationuikit

提问by Johann

I would like to have some kind of pulse animation (infinite loop "scale in - scale out") on a UIButton so it gets users' attention immediately.

我想在 UIButton 上有某种脉冲动画(无限循环“缩小 - 缩小”),以便立即引起用户的注意。

I saw this link How to create a pulse effect using -webkit-animation - outward ringsbut I was wondering if there was any way to do this only using native framework?

我看到了这个链接如何使用 -webkit-animation - 外环创建脉冲效果,但我想知道是否有任何方法可以仅使用本机框架来做到这一点?

回答by beryllium

CABasicAnimation *theAnimation;

theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=1.0;
theAnimation.repeatCount=HUGE_VALF;
theAnimation.autoreverses=YES;
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.0];
[theLayer addAnimation:theAnimation forKey:@"animateOpacity"]; //myButton.layer instead of

Swift

迅速

let pulseAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity))
pulseAnimation.duration = 1
pulseAnimation.fromValue = 0
pulseAnimation.toValue = 1
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = .greatestFiniteMagnitude
view.layer.add(pulseAnimation, forKey: "animateOpacity")

See the article "Animating Layer Content"

参见文章“动画层内容”

回答by Ravi Sharma

Here is the swift code for it ;)

这是它的快速代码;)

let pulseAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
pulseAnimation.duration = 1.0
pulseAnimation.toValue = NSNumber(value: 1.0)
pulseAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = .greatestFiniteMagnitude
self.view.layer.add(pulseAnimation, forKey: nil)

回答by Bassebus

The swift code is missing a fromValue, I had to add it in order to get it working.

swift 代码缺少一个fromValue,我必须添加它才能使其正常工作。

pulseAnimation.fromValue = NSNumber(float: 0.0)

Also forKeyshould be set, otherwise removeAnimationdoesn't work.

forKey应该设置,否则removeAnimation不起作用。

self.view.layer.addAnimation(pulseAnimation, forKey: "layerAnimation")

回答by Ahmad ELkhwaga

func animationScaleEffect(view:UIView,animationTime:Float)
{
    UIView.animateWithDuration(NSTimeInterval(animationTime), animations: {

        view.transform = CGAffineTransformMakeScale(0.6, 0.6)

        },completion:{completion in
            UIView.animateWithDuration(NSTimeInterval(animationTime), animations: { () -> Void in

                view.transform = CGAffineTransformMakeScale(1, 1)
            })
    })

}


@IBOutlet weak var perform: UIButton!

@IBAction func prefo(sender: AnyObject) {
    self.animationScaleEffect(perform, animationTime: 0.7)
}