Instance Method

addPresentedHandler(_:)

Registers a block of code that is called immediately after the drawable has been presented on the screen.

Required.

Declaration

func addPresentedHandler(_ block: @escaping MTLDrawablePresentedHandler)

Parameters

block

A block of code to be invoked.

Discussion

Multiple presented handlers may be registered for a drawable.

Listing 1 demonstrates how to use the presentedTime property to derive the drawable's presentation duration the app's frame rate.

Listing 1

Calculating presentation duration and frame rate

// Property declarations
var previousPresentedTime: CFTimeInterval = 0.0
/* ... */
// Render loop
currentDrawable.addPresentedHandler({ [weak self] drawable in
    guard let strongSelf = self else {
        return
    }
    let presentationDuration = drawable.presentedTime - strongSelf.previousPresentedTime
    let frameRate = 1.0/presentationDuration
    /* ... */
    strongSelf.previousPresentedTime = drawable.presentedTime
})

See Also

Presenting the Drawable

func present()

Presents the drawable on the screen as soon as possible.

Required.

func present(afterMinimumDuration: CFTimeInterval)

Presents the drawable on the screen after waiting for the previous drawable to meet the minimum display time.

Required.

func present(at: CFTimeInterval)

Presents the drawable on the screen at a specific host time.

Required.

typealias MTLDrawablePresentedHandler

A block of code invoked when a drawable has been presented.