旋转和动画图像视图,没有任何失真



>我目前正在尝试将图像旋转约 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
                        })
                    }

您需要"重置"转换,更改帧,然后重新应用转换:

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

最新更新