【发布时间】:2018-01-04 14:01:05
【问题描述】:
我写这个是为了将心跳动画应用到 CAShapeLayer,在这个工作正常之后,我需要在 UIButton (btnTest) 后面实现它而不缩放 UIButton 或任何其他内容。
@IBOutlet weak var btnTest: UIButton!
let btnLayer = CAShapeLayer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
btnTest.layer.removeAllAnimations()
let ovalPath = UIBezierPath(arcCenter: CGPoint(x: btnTest.frame.midX, y: btnTest.frame.midY), radius: 100, startAngle: 0*(CGFloat.pi / 180), endAngle: 360*(CGFloat.pi / 180), clockwise: true)
btnLayer.path = ovalPath.cgPath
btnLayer.fillColor = UIColor.red.cgColor
btnLayer.contentsGravity = "center"
btnLayer.opacity = 0.3
self.view.layer.addSublayer(btnLayer)
let theAnimation = CABasicAnimation(keyPath: "transform.scale.xy")
theAnimation.duration = 0.75
theAnimation.repeatCount = Float.infinity
theAnimation.autoreverses = true
theAnimation.fromValue = 1.0
theAnimation.toValue = 1.2
theAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
self.view.layer.add(theAnimation, forKey: nil)
}
这是结果gif
另一个解决方案是改变这一行:
self.view.layer.add(theAnimation, forKey: nil)
到这里:
btnLayer.add(theAnimation, forKey: nil)
结果是这样的:gif
解决这个问题的任何想法!
【问题讨论】:
标签: ios swift3 uibutton cashapelayer cabasicanimation