<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "SpriteKit",
  "identifier" : "/documentation/SpriteKit/SKAction/customAction(withDuration:actionBlock:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "SpriteKit"
    ],
    "preciseIdentifier" : "c:objc(cs)SKAction(cm)customActionWithDuration:actionBlock:"
  },
  "title" : "customAction(withDuration:actionBlock:)"
}
-->

# customAction(withDuration:actionBlock:)

Creates an action that executes a block over a duration.

```
class func customAction(withDuration duration: TimeInterval, actionBlock block: @escaping (SKNode, CGFloat) -> Void) -> SKAction
```

## Parameters

`duration`

The duration of the action, in seconds.

`block`

The block to run. The block takes the following parameters:

- node:
  The node on which the action is running.
- elapsedTime:
  The amount of time that has passed in the animation.

## Return Value

A new action object.

## Discussion

When the action executes, the block is called repeatedly until the action’s duration expires. The elapsed time is computed and passed to the block whenever the block is called.

This action is not reversible; the reverse action executes the same block.

The following Swift code shows how you can create a custom action to update an attribute of an [`SKShader`](/documentation/SpriteKit/SKShader) attached to a sprite node.

```swift
let customAction = SKAction.customAction(withDuration: 2.0) {
    node, elapsedTime in
    
    if let node = node as? SKSpriteNode {
        node.setValue(SKAttributeValue(float: Float(elapsedTime)),
                                       forAttribute: "a_time")
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)