为什么在 XCode 8 和 Swift 3 中图像大小不随动画变化?

Why Image size not changing with animation in XCode 8 and Swift 3?

我正在 iOS 应用程序中创建一个动画,其中的形状会变大并旋转。我已经创建了图像,因此它们在我的照片编辑器中具有不同的大小,但是当我为它们制作动画时,它们会保持第一张图像的大小。这是我当前的测试代码:

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
var images = [UIImage]()
var images2 = [UIImage]()


images += [#imageLiteral(resourceName: "circle1"), #imageLiteral(resourceName: "circle2"), #imageLiteral(resourceName: "circle3"), #imageLiteral(resourceName: "circle4")]
images2 += [#imageLiteral(resourceName: "circle4"), #imageLiteral(resourceName: "circle3"), #imageLiteral(resourceName: "circle2"), #imageLiteral(resourceName: "circle1")]

override func viewDidLoad() {
    super.viewDidLoad()


    imageView.animationImages = images
    imageView.animationDuration = 4.0

}

 @IBAction func startButton(_ sender: UIButton) {
    imageView.startAnimating()

    self.perform(#selector(ViewController.secondAnimation), with: nil, afterDelay: 0.9)
}

func secondAnimation() {
     imageView.stopAnimating()
     imageView.animationImages = images2
     imageView.animationDuration = 6.0
     imageView.animationRepeatCount = 1
     imageView.startAnimating()
 }

分辨率较低的图像会被拉伸并像素化以匹配其他照片的大小。它们在用户界面上都具有相同的相对大小 (UIImageView)。如果我颠倒动画的顺序,正在动画的图像将再次设置为第一张照片的默认大小,但大小不同,所以我确定动画正在使用第一个图像大小来设置默认大小显示器。

为了尽可能简化我的问题,动画显示了更改后的分辨率,但没有显示每个图像的适当相对大小变化。为什么会这样?

如果你想像动画一样使用gif,那么你可以忘记iOS动画。在图片编辑中做所有事情。 您需要做的是编辑图片,而不是代码。 把所有的图片都做成一样的尺寸,"smaller"张,背景色用纯色。

要在 iOS 中制作动画,这里有一些示例代码:

UIView.animate(withDuration: 0.5) { 
    var t = self.image.transform
    t = t.scaledBy(x: 1.3, y: 1.3)
    t = t.rotated(by: CGFloat(M_PI_4))
    self.image.transform = t;
}