我有一个想要在0.5秒后显示的UIView,并在0.5秒后再次隐藏,创建一个简单的动画.我的代码如下: let animation = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) { self.timerBackground.alpha = 1 let tran
let animation = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) { self.timerBackground.alpha = 1 let transition = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) { self.timerBackground.alpha = 0 } transition.startAnimation(afterDelay: 0.5) } animation.startAnimation()
当我测试它时,没有任何反应.我认为这是因为它们都在同一时间运行,这意味着它们相互抵消,但这不是“afterDelay”部分应该阻止的吗?
如果我单独运行它们,即从隐藏变为可见,或者从隐藏变为可见,它可以工作,但是当我尝试按顺序运行它时,它不起作用.
我的UIView不是不透明或隐藏的.
您可以使用Timer,并在每个计时器刻度上添加出现/隐藏动画块到您的UIViewPropertyAnimatorobject.这是一个代码库:
@IBOutlet weak var timerBackground: UIImageView! private var timer: Timer? private var isShown = false private var viewAnimator = UIViewPropertyAnimator.init(duration: 0.5, curve: .linear) override func viewDidLoad() { super.viewDidLoad() viewAnimator.addAnimations { self.timerBackground.alpha = 1 } viewAnimator.startAnimation() isShown = true self.timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.startReversedAction), userInfo: nil, repeats: true) } func startReversedAction() { // stop the previous animations block if it did not have time to finish its movement viewAnimator.stopAnimation(true) viewAnimator.addAnimations ({ self.timerBackground.alpha = self.isShown ? 0 : 1 }) viewAnimator.startAnimation() isShown = !isShown }
我已经为dots jumping的dots jumping实现了非常相似的行为.
请随时查看以获取更多详细信息.