見返りを強制するための返金保証付きの強化された具体的な方法 同期的に (呼び出しコードに戻る前描画はCALayer、UIViewサブクラスとの相互作用を構成することです。
UIViewサブクラスでdisplayNow()、レイヤーに「表示用にコースを設定」してから「そうする」ように。
迅速
/// Redraws the view's contents immediately.
/// Serves the same purpose as the display method in GLKView.
public func displayNow()
{
    let layer = self.layer
    layer.setNeedsDisplay()
    layer.displayIfNeeded()
}
Objective-C
/// Redraws the view's contents immediately.
/// Serves the same purpose as the display method in GLKView.
- (void)displayNow
{
    CALayer *layer = self.layer;
    [layer setNeedsDisplay];
    [layer displayIfNeeded];
}
またdraw(_: CALayer, in: CGContext)、プライベート/内部描画メソッドを呼び出すメソッドを実装します(これは、UIViewがであるため機能しますCALayerDelegate):
迅速
/// Called by our CALayer when it wants us to draw
///     (in compliance with the CALayerDelegate protocol).
override func draw(_ layer: CALayer, in context: CGContext)
{
    UIGraphicsPushContext(context)
    internalDraw(self.bounds)
    UIGraphicsPopContext()
}
Objective-C
/// Called by our CALayer when it wants us to draw
///     (in compliance with the CALayerDelegate protocol).
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    UIGraphicsPushContext(context);
    [self internalDrawWithRect:self.bounds];
    UIGraphicsPopContext();
}
カスタムを作成します internalDraw(_: CGRect)フェイルセーフとともにメソッドをdraw(_: CGRect)。
迅速
/// Internal drawing method; naming's up to you.
func internalDraw(_ rect: CGRect)
{
    // @FILLIN: Custom drawing code goes here.
    //  (Use `UIGraphicsGetCurrentContext()` where necessary.)
}
/// For compatibility, if something besides our display method asks for draw.
override func draw(_ rect: CGRect) {
    internalDraw(rect)
}
Objective-C
/// Internal drawing method; naming's up to you.
- (void)internalDrawWithRect:(CGRect)rect
{
    // @FILLIN: Custom drawing code goes here.
    //  (Use `UIGraphicsGetCurrentContext()` where necessary.)
}
/// For compatibility, if something besides our display method asks for draw.
- (void)drawRect:(CGRect)rect {
    [self internalDrawWithRect:rect];
}
そして、(コールバックなどから)myView.displayNow()本当に本当にそれを描画する必要があるときはいつでも呼び出してください。私たちのメソッドはto に通知します。これは同期的に私たちにコールバックしてで描画を行い、次に進む前にコンテキストに描画されたものでビジュアルを更新します。CADisplayLinkdisplayNow()CALayerdisplayIfNeeded()draw(_:,in:)internalDraw(_:)
このアプローチは、上記の@RobNapierと似ていますが、displayIfNeeded()に加えて呼び出しを行うという利点があり、setNeedsDisplay()同期が行われます。
これは、 CALayer sがsよりも多くの描画機能を公開しているUIViewレイヤーはビューよりも低レベルであり、レイアウト内で高度に構成可能な描画を目的として明示的に設計されており、(Cocoaの多くのものと同様に)柔軟に使用できるように設計されています(親クラスとして、または委譲者として、または他の描画システムへの橋渡しとして、またはそれら自身で)。CALayerDelegateプロトコルの適切な使用により、これらすべてが可能になります。
の構成可能性CALayerの詳細については、 『コアアニメーションプログラミングガイド』の「レイヤーオブジェクトの設定」セクションを参照してください。