我希望能在UIButton上实现一种脉冲动画(无限循环“放大 - 缩小”),以便立即引起用户注意。
我看到了这个链接如何使用-webkit动画创建脉冲效果 - 外向环,但我想知道是否有任何方法只使用本地框架来实现此目的?
我希望能在UIButton上实现一种脉冲动画(无限循环“放大 - 缩小”),以便立即引起用户注意。
我看到了这个链接如何使用-webkit动画创建脉冲效果 - 外向环,但我想知道是否有任何方法只使用本地框架来实现此目的?
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")
这是它的 Swift 代码;)
let pulseAnimation = CABasicAnimation(keyPath: "transform.scale")
pulseAnimation.duration = 1.0
pulseAnimation.fromValue = NSNumber(value: 0.0)
pulseAnimation.toValue = NSNumber(value: 1.0)
pulseAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = .greatestFiniteMagnitude
self.view.layer.add(pulseAnimation, forKey: nil)
Swift代码缺少fromValue
,我不得不添加它以使其正常工作。
pulseAnimation.fromValue = NSNumber(value: 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)
}
myView.layer
即可访问它。您可以使用核心动画来对层进行动画处理。对于缩放变换,您可以使用此方法:结构字段的关键路径支持。 - beryllium#import <QuartzCore/QuartzCore.h>
以获取CALayer的所有定义。 - progrmr