我想制作一个动画,将背景图像从右向左移动而不是从左向右移动。 我使用此代码但不起作用
UIView.animate(withDuration: 5, delay: 0, options: .repeat ,animations: { () -> Void in
imageView.transform = CGAffineTransform(translationX: -imageView.frame.width + self.view.bounds.width, y: 0)
UIView.animate(withDuration: 5, delay: 1, options: .repeat ,animations: { () -> Void in
imageView.transform = CGAffineTransform(translationX: +imageView.frame.width - self.view.bounds.width, y: 0)
})
})
答案 0 :(得分:0)
如果要使用旧版animate(withDuration:animations:)
API,请使用以下代码。但是,您需要等待第一个动画完成-这意味着您必须使用completion
块。
但是,您为translationX
设置的值也没有意义。
UIView.animate(withDuration: 1, animations: {
self.imageView.transform = CGAffineTransform(translationX: -self.imageView.frame.width, y: 0)
}) { _ in
UIView.animate(withDuration: 1) {
self.imageView.transform = CGAffineTransform.identity
}
}
我强烈建议您看一下动画的“新”制作方式:UIViewPropertyAnimator
(https://developer.apple.com/documentation/uikit/uiviewpropertyanimator)
答案 1 :(得分:0)
我解决了
UIView.animate(withDuration: 30.0, delay: 0, options: [.repeat, .autoreverse], animations: {
imageView.transform = CGAffineTransform(translationX: -imageView.frame.width + self.view.bounds.width, y: 0)
}, completion: nil)