为 UIView 设置动画以从屏幕上移出然后从底部无休止地移出屏幕

Posted

技术标签:

【中文标题】为 UIView 设置动画以从屏幕上移出然后从底部无休止地移出屏幕【英文标题】:Animate UIView to go up off screen then up off screen from bottom endlessly 【发布时间】:2019-04-10 20:23:12 【问题描述】:

我的问题与this 类似。但是,我想要一个 swift 的更新版本,并且我希望它无休止地运行。这是我现在的动画:

bubble.frame.origin = CGPoint(x: 75, y: -120)

UIView.animate(
    withDuration: 2.5,
    animations: 
        self.bubble.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height * -1.3)
    
)

气泡离开屏幕后,我希望它从底部重新开始,一次又一次地向上离开屏幕。

【问题讨论】:

developer.apple.com/documentation/uikit/uiview/1622451-animate 【参考方案1】:

使用 UIViewAnimationOptions 集中的选项:

UIViewAnimationOptions.Repeat

您的代码应该如下所示:

bubble.frame.origin = CGPoint(x: 75, y: -120)
UIView.animate(withDuration: 2.5, options: [UIViewAnimationOptions.Repeat], animations: 
               self.bubble.transform = CGAffineTransform(translationX: 0, y: self.view.frame.height * -1.3)
               )

【讨论】:

这适用于哪个版本的 swift?我正在使用 swift 4,但这不起作用。 试试options: [.Repeat] @Slaknation 如果它不起作用,那意味着你做错了什么

以上是关于为 UIView 设置动画以从屏幕上移出然后从底部无休止地移出屏幕的主要内容,如果未能解决你的问题,请参考以下文章

从屏幕右侧向左滑动 UIView

在 Swift 中,如何使用自动布局为 UIView 设置动画,就像页面滑入一样?

通过更新约束滑入 UIView

如何创建设置活动以从底部显示屏幕的一半? [关闭]

使用动画更改 UIView 背景颜色

从左到右将 UIView 移出屏幕并从左再次将其移入