Swift 快速将图像从右向左移动,而不是从左向右移动
我想制作一个动画,将背景图像从右向左移动,而不是从左向右移动。 我使用此代码,但不起作用Swift 快速将图像从右向左移动,而不是从左向右移动,swift,xcode,animation,uiviewanimation,cgaffinetransform,Swift,Xcode,Animation,Uiviewanimation,Cgaffinetransform,我想制作一个动画,将背景图像从右向左移动,而不是从左向右移动。 我使用此代码,但不起作用 UIView.animate(withDuration: 5, delay: 0, options: .repeat ,animations: { () -> Void in imageView.transform = CGAffineTransform(translationX: -imageView.frame.width + self.view.b
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)
})
})
如果要使用传统的
动画(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
()我解决了这个问题
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)
请检查这个:-