- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
UIView 关键帧动画可让您创建通过一系列步骤进行的 View 动画。函数 animateKeyframes(withDuration:delay:options:animations:completion:)
有一个类型为 UIView.KeyframeAnimationOptions
的选项参数。我希望这能让我选择一条整体时间曲线以应用于组合的动画集。它似乎应用了默认的缓入缓出动画曲线,并且似乎不允许您像 UIView.animate()
系列方法那样指定时间曲线。如果我想要线性计时,只是缓入或缓出怎么办?
最佳答案
有趣的是,UIView.animate()
方法(UIView.AnimationOptions
类型)的动画选项如果你能弄清楚如何将它们传递给 就可以工作>animateKeyframes(withDuration:delay:options:animations:completion:)
。遗憾的是,对于线性、缓入或缓出等时序曲线,没有现成的常量。 This SO answer显示如何将 UIView.AnimationOptions
常量中的值“强制”到 UIView.KeyframeAnimationOptions
OptionSet:
extension UIViewKeyframeAnimationOptions {
init(animationOptions: UIViewAnimationOptions) {
rawValue = animationOptions.rawValue
}
}
我更进一步,为不同的计时函数定义了常量。 (我不知道为什么 Apple 没有定义这些。这看起来很荒谬,但你就是这样。)
我对 UIView.KeyframeAnimationOptions
的扩展如下所示:
extension UIView.KeyframeAnimationOptions {
static var curveLinear: UIView.KeyframeAnimationOptions =
UIView.KeyframeAnimationOptions(rawValue:UIView.AnimationOptions.curveLinear.rawValue)
static var curveEaseInOut: UIView.KeyframeAnimationOptions =
UIView.KeyframeAnimationOptions(rawValue:UIView.AnimationOptions.curveEaseInOut.rawValue)
static var curveEaseIn: UIView.KeyframeAnimationOptions =
UIView.KeyframeAnimationOptions(rawValue:UIView.AnimationOptions.curveEaseIn.rawValue)
static var curveEaseOut: UIView.KeyframeAnimationOptions =
UIView.KeyframeAnimationOptions(rawValue:UIView.AnimationOptions.curveEaseOut.rawValue)
init(animationOptions: UIView.AnimationOptions) {
self.init(rawValue: animationOptions.rawValue)
}
}
通过该扩展,您可以像调用 UIView.animate()
方法一样使用时序曲线值:
UIView.animateKeyframes(withDuration: 2.0, delay: 0, options: [.curveLinear]) {
// Your animation keyframe steps here
}
UIView.AnimationOptions 中定义的一些其他标志可能也对 UIView.KeyframeAnimationOptions
有效。您可以使用允许您将 UIView.AnimationOptions
映射到 UIView.KeyframeAnimationOptions
的方法,或者向 UIView.KeyframeAnimationOptions
添加额外的标志将是微不足道的> 与我添加时序曲线标志的方式相同。
我对 UIView.animate()
和 animateKeyframes()
匹配选项中的标志感到好奇。我写了一些代码来记录两个 OptionSet 的所有值,这就是我得到的:
KeyframeAnimationOptions:
option value 0x00000001 = layoutSubviews
option value 0x00000002 = allowUserInteraction
option value 0x00000004 = beginFromCurrentState
option value 0x00000008 = repeat
option value 0x00000010 = autoreverse
option value 0x00000020 = overrideInheritedDuration
option value 0x00000200 = overrideInheritedOptions
option value 0x00000000 = calculationModeLinear
option value 0x00000400 = calculationModeDiscrete
option value 0x00000800 = calculationModePaced
option value 0x00000C00 = calculationModeCubic
option value 0x00001000 = calculationModeCubicPaced
UIView.AnimationOptions:
option value 0x00000001 = layoutSubviews
option value 0x00000002 = allowUserInteraction
option value 0x00000004 = beginFromCurrentState
option value 0x00000008 = repeat
option value 0x00000010 = autoreverse
option value 0x00000020 = overrideInheritedDuration
option value 0x00000200 = overrideInheritedOptions
option value 0x00000040 = overrideInheritedCurve
option value 0x00000080 = allowAnimatedContent
option value 0x00000100 = showHideTransitionViews
option value 0x00000000 = curveEaseInOut
option value 0x00010000 = curveEaseIn
option value 0x00020000 = curveEaseOut
option value 0x00030000 = curveLinear
option value 0x00100000 = transitionFlipFromLeft
option value 0x00200000 = transitionFlipFromRight
option value 0x00300000 = transitionCurlUp
option value 0x00400000 = transitionCurlDown
option value 0x00500000 = transitionCrossDissolve
option value 0x00600000 = transitionFlipFromTop
option value 0x00700000 = transitionFlipFromBottom
option value 0x03000000 = preferredFramesPerSecond60
option value 0x07000000 = preferredFramesPerSecond30
两组中的前 7 个标志具有相同的名称和相同的值。 KeyframeAnimationOptions
的 0 值是 calculationModeLinear
,它是 UIView.AnimationOptions 的 curveEaseInOut
。它会导致关键帧和“常规”UIView 动画的缓入/缓出时间。
所有其他常量的值在两个 OptionSet 中都是唯一的,这表明其他一些 UIView.AnimationOptions
可能适用于关键帧动画。不过,我只测试了时序曲线值,它们都按预期工作。
关键帧动画标志(如 calculationModeLinear、calculationModeCubic 和 calculationModeCubicPaced)和 View 动画标志(如 curveLinear、curveEaseInOut 等)将如何交互并不是很明显,因为关键帧动画标志也与动画计时相关.我写了一个测试应用程序看看他们是怎么做的。事实证明,UIView.AnimationOptions 时序曲线标志会影响整个关键帧动画的整体时序。 (整体关键帧动画开始和停止的方式。)
您可以从github 下载示例项目here .
很难看到缓入/缓出 UIView 动画计时与立方关键帧动画选项之一相结合。使用关键帧计算模式线性模式更容易看到缓入/缓出。
下面是该组合的视频:
(请注意青色方 block 在返回起点之前如何滑行到动画矩形的右下角停止。)
关于ios - 如何为 UIView 关键帧动画设置动画曲线 (`func UIView.animateKeyframes()` ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68056506/
我正在尝试一个关键帧动画,其中 View 淡入,然后转换(平移和缩放),然后在延迟后再次转换,然后淡出。我遇到的问题是第二次转换在第一次转换结束后立即开始,而不是尊重延迟值。这似乎只发生在它再次修改变
我正在尝试对标签进行动画处理,以在不同关键帧处使用不同的单词更改其文本,但它只是跳到最后一个关键帧(“晚安”),而不显示之前的关键帧。它确实打印了它们,所以我猜持续时间或开始时间有问题,但我不知道它可
我使用以下代码在 Swift 3 中使用 animateKeyframes 为按钮的比例设置动画: UIView.animateKeyframes(withDuration: 4, delay: 1,
目前有 2 个 CGAffineTransform 关键帧添加到 UIView.animateKeyframes。由于某些奇怪的原因,第二个关键帧的动画似乎没有发生。它只是在动画完成后立即跳回起始位置
我正在使用关键帧为标签创建一个简单的从左到右的动画,但是当动画重复时,延迟将被忽略。 第一次执行时,延迟3秒有效果,但当动画重复时,延迟被忽略。这会导致动画在结束后立即重新开始。 UIView.ani
我正在尝试为飞机起飞场景制作动画。我在这里使用 UIView animatekeyFrames 方法并提供所需的关键帧,但最后一个关键帧没有动画,我附上了一个 GIF,您可以在其中看到最终翻译中的故障
我正在尝试通过减少宽度约束来为 UIView 设置动画,但在使用 UIView.animateKeyFrames 时效果不佳。这是我的代码: UIView.animateKeyframes(w
我要么是愚蠢,要么误解了关键帧动画在 iOS 上的工作方式(或两者!)。下面的两个动画 block 产生不同的结果,但我希望它们是相同的: let duration: TimeInterval = 2
UIView 关键帧动画可让您创建通过一系列步骤进行的 View 动画。函数 animateKeyframes(withDuration:delay:options:animations:comple
我有两个 View Controller 。 AnimationVC 有一些 UIView 动画,而 DestinationVC 没有。 我有 CPU 使用问题。在我执行 segue 之后,动画 bl
我是一名优秀的程序员,十分优秀!