Dani Kemper
Dani Kemper

Reputation: 343

Animate a button to the left and right with 20 pixels

I want to have my button moved with 20 pixels to the left and I want it after 0.25 second back at the original spot. This is what i got so far:

@IBOutlet weak var Like: UIButton!

UIView.animate(withDuration: 0.25,
                       animations: {
                        var likeframe = self.Like.frame
                        likeframe.origin.x -= 20
        },
                       completion: { _ in
                        UIView.animate(withDuration: 0.25) {
                            var likeframe = self.Like.frame
                            likeframe.origin.x += 20

        }
})

Please help!

Upvotes: 1

Views: 46

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need to change the frame itself

UIView.animate(withDuration: 0.25,animations: {
          self.Like.frame =  self.Like.frame.offsetBy(dx:-20,dy:0)

        }) { _ in

           UIView.animate(withDuration: 0.25 , animations: {
            self.Like.frame =  self.Like.frame.offsetBy(dx:20,dy:0)

           })
}

Upvotes: 2

user4527951
user4527951

Reputation:

Change the frame and use the delay function to delay the animation

UIView.animate(withDuration: 0.25,
               animations: {
                Like.frame.origin.x -= 20

},
               completion: { _ in
                UIView.animate(withDuration: 0.25, delay: 0.25, options: [], animations: {
                    Like.frame.origin.x += 20
                }, completion: nil)
})

Upvotes: 0

Related Questions