【发布时间】:2011-12-26 08:33:32
【问题描述】:
我想在 UIButton 上制作某种脉冲动画(无限循环“缩小 - 缩小”),以便立即引起用户的注意。
我看到了这个链接How to create a pulse effect using -webkit-animation - outward rings,但我想知道是否有任何方法可以只使用本机框架来做到这一点?
【问题讨论】:
标签: ios objective-c animation uikit
我想在 UIButton 上制作某种脉冲动画(无限循环“缩小 - 缩小”),以便立即引起用户的注意。
我看到了这个链接How to create a pulse effect using -webkit-animation - outward rings,但我想知道是否有任何方法可以只使用本机框架来做到这一点?
【问题讨论】:
标签: ios objective-c animation uikit
这是它的快速代码;)
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")
【讨论】:
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: CAMediaTimingFunctionName.easeInEaseOut)
pulseAnimation.autoreverses = true
pulseAnimation.repeatCount = .greatestFiniteMagnitude
view.layer.add(pulseAnimation, forKey: "animateOpacity")
【讨论】:
myView.layer 即可访问它。您可以使用 Core Animation 为图层设置动画。对于比例转换,您可以使用这种方法:Key Path Support for Structure Fields
#import <QuartzCore/QuartzCore.h> 以获取CALayers 的所有定义。
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)
}
【讨论】: