何时准备使用CALayer.timeOffset [英] When is CALayer.timeOffset ready to be used

查看:144
本文介绍了何时准备使用CALayer.timeOffset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在高级CALayer问题上需要一些帮助。

Need some help with an advanced CALayer issue.

我有一个添加CAShapeLayer的UIView。图层已暂停(速度= 0),并附加了我要手动控制的动画。

I have a UIView that adds a CAShapeLayer. The layer is paused (speed = 0), and it has an animation attached that I want to manually control.

非常类似于此处:在两个CGPath / UIBeziers之间进行补间/内插-除了我的视图是集合视图单元格。

Very similar to here: Tweening / Interpolating between two CGPaths / UIBeziers - Except my view is a collection view cell.

一切正常,除了我不想从0位置开始。每个单元格可能在动画中的不同位置开始。

Everything works as expected, except I don't want to start from the 0 position. Each cell might start in a different position in the animation.

当集合视图要求我提供视图时,我正在尝试配置layer.timeOffset 0.5,而不是0。这是行不通的,我尝试了所有可以找到的View和Layer以及委托回调。

When the collection view asks me for my view, I'm trying to configure the layer.timeOffset to be say 0.5 rather than 0. This doesn't work, and I tried every View and Layer and delegate callbacks I could find.

我发现的唯一解决方法是:
dequeueReusableCellWithIdentifier 而不是设置 cellView.subLayer.timeOffset = 0.5; ,我将更新分派到以后: / p>

The only workaround I found was in: dequeueReusableCellWithIdentifier instead of setting the cellView.subLayer.timeOffset = 0.5;, I dispatch the update to later:

dispatch_after(main_queue, a bit later, ^{
  cellView.subLayer.timeOffset = 0.5
});

所以我的问题是,图层/动画的viewDidLoad等效于什么。什么时候在该层上调用timeOffset真正起作用?

So my question is, what is the equivalent of a viewDidLoad for Layers / Animations. When would calling timeOffset on the layer actually work?

有人遇到这个问题吗?

推荐答案

通过操纵动画的初始时间偏移找到了多个(可能不正确的)解决方案。

Found multiple (probably incorrect) solutions by manipulating the animations initial timeOffset.

最终,我决定不依赖动画来渲染初始状态。所有。相反,我在动画之外设置了正确的属性值,并且仅在用户交互期间在实际需要时才添加动画,并在完成后将其删除。

Eventually I decided not to rely on the animation for rendering the initial states at all. Instead, I'm setting the correct property values outside of the animation, and only adding the animations when they are actually needed, during user interactions, and removing them once done.

这意味着还有更多代码,但至少是正确的。层和媒体计时很棘手。我在上面找到的最好的文章是

This means there's a bit more code, but at least it is correct. Layer and media timing are tricky. The best articles I found on it are here:

  • https://coveller.com/2016/05/core_animation_timing
  • http://ronnqvi.st/controlling-animation-timing

如果您需要这样做,我强烈建议按照Apple的建议暂停并恢复该图层:

If you need to do this, I strongly recommend pausing and resuming the layer as Apple recommends:

  • https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html#//apple_ref/doc/uid/TP40004514-CH8-SW15

另外,值得一提的是,使用CATra nsaction在修改任何可动画设置的属性时至关重要,有时在文档和示例中被忽略。

Also, worth pointing out, using CATransaction's when modifying any of the animatable properties was crucial, and sometimes overlooked in documentations and examples.

这篇关于何时准备使用CALayer.timeOffset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆