快速从右向左移动图像,而不是从左向右移动图像

时间:2020-03-06 11:31:02

标签: 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.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)

                })

            })

2 个答案:

答案 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
            }
        }

我强烈建议您看一下动画的“新”制作方式:UIViewPropertyAnimatorhttps://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)
相关问题