When is CALayer.timeOffset ready to be used

江枫思渺然 提交于 2019-12-23 02:20:02

问题


Need some help with an advanced CALayer issue.

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.

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

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.

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.

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
});

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

Did anyone have this problem?


回答1:


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

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

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



来源:https://stackoverflow.com/questions/45388818/when-is-calayer-timeoffset-ready-to-be-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!