图像完全相同。我想使用.repeat选项从右到左制作动画,因此它永远不会完成。该代码可以正常工作,但是问题是当我尝试在后台运行应用程序后恢复动画状态时。动画仍在继续,但是就像图像2将会完成,然后只有背景,而没有图像(照片#2)。
viewDidLoad方法:
secondImage.transform = CGAffineTransform.identity.translatedBy(x: +self.view.bounds.width + 440, y: 0)
NotificationCenter.default.addObserver(self, selector: #selector(self.didEnterBackground), name: .UIApplicationDidEnterBackground, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.didEnterForeground), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
didEnterBackground方法
firstImageTransform = firstImage.layer.presentation()?.affineTransform() ?? .identity
secondImageTransform = secondImage.layer.presentation()?.affineTransform() ?? .identity
didEnterForeground方法
self.firstImage.transform = firstImageTransform
self.secondImage.transform = CGAffineTransform.identity.translatedBy(x: +(self.firstImage.frame.origin.x + self.firstImage.frame.width ), y: 0)
self.animate()
动画方法
UIView.animate(withDuration: 15, delay: 0, options: [.repeat, .beginFromCurrentState, .curveLinear], animations: { [unowned self] in
var transform = self.firstImage.transform
transform = transform.concatenating(CGAffineTransform(translationX: -(self.view.bounds.width + 440), y: 0))
self.firstImage.transform = transform
var transformSecond = self.secondImage.transform
transformSecond = transformSecond.concatenating(CGAffineTransform(translationX: -(self.view.bounds.width + 440), y: 0))
self.secondImage.transform = transformSecond }, completion: nil)