[英]Swift move image from right to left than from left to right
我想制作一個從右向左移動背景圖像而不是從左向右移動的動畫。 我使用此代碼但不起作用
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)
})
})
如果您想使用舊版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 )
我用這個解決了
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.