ios – 如何在UIButton上实现本机“脉冲效果”animation
我想在UIButton上有一些脉冲animation(无限循环“缩放”),所以立即引起用户的注意。
我看到这个链接如何创build一个使用-webkitanimation – 脉冲效应向外环,但我想知道是否有任何方式只使用本机框架做到这一点?
任何帮助非常感谢 – 代码示例甚至更好;-)
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
迅速
let pulseAnimation = CABasicAnimation(keyPath: #keyPath(CALayer.opacity)) pulseAnimation.duration = 1 pulseAnimation.fromValue = 0 pulseAnimation.toValue = 1 pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) pulseAnimation.autoreverses = true pulseAnimation.repeatCount = .greatestFiniteMagnitude self.view.layer.addAnimation(pulseAnimation, forKey: "animateOpacity")
看到这篇文章
这是它的快速代码;)
let pulseAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale") pulseAnimation.duration = 1.0 pulseAnimation.toValue = NSNumber(value: 1.0) pulseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) pulseAnimation.autoreverses = true pulseAnimation.repeatCount = FLT_MAX self.view.layer.add(pulseAnimation, forKey: nil)
swift代码缺less一个fromValue
,我不得不添加它才能使它工作。
pulseAnimation.fromValue = NSNumber(float: 0.0)
还应该设置forKey
,否则removeAnimation
不起作用。
self.view.layer.addAnimation(pulseAnimation, forKey: "layerAnimation")
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) }