旋转和动画ImageView没有任何失真?

时间:2018-01-10 17:59:15

标签: ios swift animation

我目前正在尝试将图像旋转约13度,然后将该图像从屏幕左侧“划线”到左侧。它不是按照我想要的方式工作 - 一旦图像旋转,我试图动画任何更多的动作,它通过拉伸它来扭曲图像,直到它只有一个像素!

如何在不发生任何失真的情况下保留图像的宽度和高度?我尝试了布局约束并以编程方式设置frame.size(宽度和高度),但这也不起作用。这是代码:

 @IBOutlet weak var astro: UIImageView!
 UIView.animate(withDuration: 1.5, animations: {
                    self.astro.transform = CGAffineTransform(rotationAngle: -13 * 3.14/180.0)
                    self.view.layoutIfNeeded()
                }, completion: { (success) in
                    if(success == true){
                        UIView.animate(withDuration: 1.5, animations: {
                            self.astro.frame.origin.x = -100
                            self.astro.frame.origin.y = 0
                            self.astro.frame.size.height = 119
                            self.astro.frame.size.width = 108
                        })
                    }

1 个答案:

答案 0 :(得分:1)

你需要"重置"转换,更改框架,然后重新应用转换:

    let rot = CGAffineTransform(rotationAngle: -13 *  3.14/180.0)

    UIView.animate(withDuration: 1.5, animations: {
        self.astro.transform = rot
    }, completion: { (success) in
        if success == true {
            UIView.animate(withDuration: 1.5, animations: {
                self.astro.transform = .identity
                self.astro.frame.origin.x = -100.0
                self.astro.transform = rot
            })
        }
    })