Align an image horizontally and vertically in swift
<p><a href="https://i.stack.imgur.com/7Lc7a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Lc7a.png" alt="enter image description here"></a></p>

<p>I want to align an image in another (horizontally and vertically) to create an animation. The animation is simple: the heart will "vibrate" inside the Angry Bedou's logo, but he must be "centered" in that image.</p>

<p>I was trying the following: </p>

<pre><code>    func animate() {
    heartImg.frame = CGRectMake(angryBedousImg.frame.size.width / 2, angryBedousImg.frame.size.height / 2, heartImg.frame.size.width, heartImg.frame.size.height)

    let heartAnimation = CABasicAnimation(keyPath: "position")
    heartAnimation.duration = 0.09
    heartAnimation.repeatCount = .infinity
    heartAnimation.autoreverses = true
    heartAnimation.fromValue = NSValue(CGPoint: CGPointMake(heartImg.center.x - 1, heartImg.center.y))
    heartAnimation.toValue = NSValue(CGPoint: CGPointMake(heartImg.center.x + 1, heartImg.center.y))
    heartImg.layer.addAnimation(heartAnimation, forKey: "position")
        }
</code></pre>

<p>But it wont centralizes when i build the app. The animation works, but not the alignment. I want the alignment work in every device in portrait mode. </p>

<p><a href="https://i.stack.imgur.com/D0Hou.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D0Hou.png" alt="enter image description here"></a></p>

<p>I look at the storyboard, and the images are fine. What i'm doing wrong?</p>