从右向左比从左向右快速移动图像

问题描述 投票:0回答:2

我想制作一个动画,将背景图像从右向左移动而不是从左向右移动。我使用此代码,但不起作用

        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)

                })

            })
swift xcode animation uiviewanimation cgaffinetransform
2个回答
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


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)
© www.soinside.com 2019 - 2024. All rights reserved.