title: CABasicAnimation to emulate a 'pulse' effect animation on a non-circle shape
body: <p>I am using CBasicAnimation to create a pulsating effect on a button.
  The effect pulses out the shape of a UIView, with border only.</p>
  
  <p>While the animation works properly, I am not getting the desired effect using <code>CABasicAnimation(keyPath: "transform.scale")</code>.</p>
  
  <p>I am using an animation group with 3 animations: borderWidth, transform.scale and opacity.</p>
  
  <pre><code>class Pulsing: CALayer {
  
  var animationGroup = CAAnimationGroup()
  
  var initialPulseScale:Float = 1
  var nextPulseAfter:TimeInterval = 0
  var animationDuration:TimeInterval = 1.5
  var numberOfPulses:Float = Float.infinity
  
  
  override init(layer: Any) {
      super.init(layer: layer)
  }
  
  required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
  }
  
  
  init (numberOfPulses:Float = Float.infinity, position:CGPoint, pulseFromView:UIView, rounded: CGFloat) {
      super.init()
  
  
      self.borderColor = UIColor.black.cgColor
  
  
  
      self.contentsScale = UIScreen.main.scale
      self.opacity = 1
      self.numberOfPulses = numberOfPulses
      self.position = position
  
      self.bounds = CGRect(x: 0, y: 0, width: pulseFromView.frame.width, height: pulseFromView.frame.height)
      self.cornerRadius = rounded
  
  
      DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
          self.setupAnimationGroup(view: pulseFromView)
  
          DispatchQueue.main.async {
               self.add(self.animationGroup, forKey: "pulse")
          }
      }
  
  
  
  }
  
  func borderWidthAnimation() -&gt; CABasicAnimation {
      let widthAnimation = CABasicAnimation(keyPath: "borderWidth")
      widthAnimation.fromValue = 2
      widthAnimation.toValue = 0.5
      widthAnimation.duration = animationDuration
  
      return widthAnimation
  }
  
  
  func createScaleAnimation (view:UIView) -&gt; CABasicAnimation {
      let scale = CABasicAnimation(keyPath: "transform.scale")
      DispatchQueue.main.async {
              scale.fromValue = view.layer.value(forKeyPath: "transform.scale")
      }
  
      scale.toValue = NSNumber(value: 1.1)
      scale.duration = 1.0
      scale.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  
      return scale
  }
  
  func createOpacityAnimation() -&gt; CABasicAnimation {
      let opacityAnimation = CABasicAnimation(keyPath: "opacity")
      opacityAnimation.duration = animationDuration
      opacityAnimation.fromValue = 1
      opacityAnimation.toValue = 0
      opacityAnimation.fillMode = .removed
  
      return opacityAnimation
  }
  
  func setupAnimationGroup(view:UIView) {
      self.animationGroup = CAAnimationGroup()
      self.animationGroup.duration = animationDuration + nextPulseAfter
      self.animationGroup.repeatCount = numberOfPulses
  
  
      self.animationGroup.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.default)
  
      self.animationGroup.animations = [createScaleAnimation(view: view), borderWidthAnimation(), createOpacityAnimation()]
  }
  
  
  
  }
  
  
  
  class ViewController: UIViewController {
  
  
  @IBOutlet weak var pulsingView: UIView!
  
  let roundd:CGFloat = 20
  
  override func viewDidLoad() {
      super.viewDidLoad()
      pulsingView.layer.cornerRadius = roundd
      let pulse = Pulsing(
          numberOfPulses: .greatestFiniteMagnitude,
          position: CGPoint(x: pulsingView.frame.width/2,
                            y: pulsingView.frame.height/2)
          , pulseFromView: pulsingView, rounded: roundd)
  
      pulse.zPosition = -10
      self.pulsingView.layer.insertSublayer(pulse, at: 0)
  }
  
  
  
  
  }
  </code></pre>
  
  <p>My problem is transform.scale is maintaining the aspect ratio of the UIView it's pulsating from during the animation.</p>
  
  <p><strong>How can I make the pulse grow so there's uniform spacing on both the height and the width? See screenshot.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/xrVqX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xrVqX.jpg" alt="Scaling of button"></a></p>