简体   繁体   English

立即更新CALayer子图层

[英]Update CALayer sublayers immediately

I have UIView which has the following structure: 我有UIView,它具有以下结构:

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

For layer I set needsDisplayOnBoundsChange = true. 对于图层 I,设置needsDisplayOnBoundsChange = true。 When I change size of UIView, layer redraws immediately. 当我更改UIView的大小时, 图层会立即重绘。 But their sublayers updates with some delay (on the next draw iteration). 但他们的子层更新有一些延迟(在下一次绘制迭代)。 How to sync draw of layers with sublayers depthLayer and bodyLayer ? 如何使用子层depthLayerbodyLayer同步绘制图层

UIView: UIView的:

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

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). 您在CALayer's frames中看到的延迟是因为动画 - >当您对CALayer's外观进行更改时它会尝试对其进行动画处理(并且它经常成功 - >这就是他们称为动画图层的原因)。

To disable this behavior (animations) you should call the disable transactions like this: 要禁用此行为(动画),您应该调用禁用事务,如下所示:

CATransaction.setValue(kCFBooleanTrue, forKey:kCATransactionDisableActions) //..Make Change to the frames here CATransaction.commit()

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. 另外我不知道你是否真的需要覆盖drawRect - >如果你通过setFrame:方法设置UIView's框架,最好覆盖setFrame:方法本身并调整子框架的框架。

Lot of Luck! 很多运气!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM