Animation to scale and move UIView (swift)

前端 未结 2 366
旧巷少年郎
旧巷少年郎 2021-01-30 16:41

I have a UIView, placed in the middle of the screen. When the user presses a button, I want it to move up near top of the screen as it shrinks to about a fifth of its size.

2条回答
  •  无人及你
    2021-01-30 17:17

    Joe's answer above does exactly as his GIF describes but it doesn't really answer your question since it translates then scales the view (as opposed to both translating and scaling at the same time). Your issue is that you're setting the view's transform in your animation block then immediately overwritting that value with another transform. To achieve both translation and scale at the same time, you'll want something like this:

    @IBAction func animateButton(_ sender: UIButton) {
        let originalTransform = self.main.transform
        let scaledTransform = originalTransform.scaledBy(x: 0.2, y: 0.2)
        let scaledAndTranslatedTransform = scaledTransform.translatedBy(x: 0.0, y: -250.0)
        UIView.animate(withDuration: 0.7, animations: {
            self.main.transform = scaledAndTranslatedTransform
        })
    }
    

提交回复
热议问题