缩放和移动UIView的动画(快速) [英] Animation to scale and move UIView (swift)

查看:644
本文介绍了缩放和移动UIView的动画(快速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView,位于屏幕中间。当用户按下按钮时,我希望它在屏幕顶部缩小到大约五分之一时向上移动。

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.

我已经尝试过:

UIView.animateWithDuration(0.7) { () -> Void in
            self.main.transform = CGAffineTransformMakeScale(0.2, 0.2)
            self.main.transform = CGAffineTransformMakeTranslation(0, -250)
        }

但是由于某些原因,这只会缩放视图。我也尝试将其放在animateWithDuration中:

But for some reason, that only scales the view. I have also tried to put this in the animateWithDuration:

self.main.frame = CGRectMake(self.view.frame.width/2 - 10, 50, 50, 50)

如何使两个动画都能正常工作?

How can I get both animations to work?

推荐答案

Joe的上述回答与他的GIF所描述的完全一样,但由于它翻译了 then 缩放视图(与同时平移和缩放相反)。问题是您要在动画块中设置视图的变换,然后立即用另一个变换覆盖该值。要同时实现翻译和缩放,您需要这样的东西:

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
    })
}

这篇关于缩放和移动UIView的动画(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆