立即更新CALayer子层 [英] Update CALayer sublayers immediately

查看:59
本文介绍了立即更新CALayer子层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有以下结构的UIView:

I have UIView which has the following structure:

UIView
  |- layer (CALayer)
    |- depthLayer (CALayer)
    |- bodyLayer (CALayer)

对于我设置了needsDisplayOnBoundsChange = true。当我更改UIView的大小时, layer 会立即重绘。但是它们的子层会有一些延迟更新(在下一次绘制迭代中)。如何将 layers 的绘制与子层 depthLayer bodyLayer 同步?

For layer I set needsDisplayOnBoundsChange = true. When I change size of UIView, layer redraws immediately. But their sublayers updates with some delay (on the next draw iteration). How to sync draw of layers with sublayers depthLayer and bodyLayer?

UIView:

override func drawRect(rect: CGRect) {
    bodyLayer.frame = rect
    depthLayer.frame = CGRectOffset(rect, 0, 5)
}


推荐答案

您在 CALayer的帧中看到的延迟是由于
动画->更改 CALayer的外观
尝试对其进行动画处理(并且通常会成功->这就是为什么他们将其称为动画层)。

The delays you see in your CALayer's frames are cause because the Animations -> when you making a change to the CALayer's appearance It tries to animate it (and it often successes -> well this is why they called animations layers).

要禁用此功能行为(动画),您应该这样调用禁用事务:

To disable this behavior (animations) you should call the disable transactions like this:

CATransaction.setValue(kCFBooleanTrue,forKey:kCATransactionDisableActions)
// ..在此处更改帧
CATransaction.commit()

我也不知道您是否拥有真正的需要覆盖 drawRect
fo r这件事->如果您通过 setFrame:方法设置 UIView的框架,则最好使用
覆盖 setFrame:方法本身,并在那里调整subLayers的框架。

Also I don't know wether you have a real need to override the drawRect for this matter -> if you set the UIView's Frame via setFrame: method, it is better to override the setFrame: method itself and adjust the Frames of subLayers there.

运气不好!

这篇关于立即更新CALayer子层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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