Contents

handle(_:forProperties:handler:)

Adds a block that modifies particle properties, to be executed at a specified event in the lifetimes of particles in the system.

Declaration

func handle(_ event: SCNParticleEvent, forProperties properties: [SCNParticleSystem.ParticleProperty], handler block: @escaping  SCNParticleEventBlock)

Parameters

  • event:

    The event at which to call the block. See Scnparticleevent for allowed values.

  • properties:

    An array containing one or more of the constants listed in Particle Property Keys, each of which specifies a property of the appearance or behaviors of particles in the particle system.

  • block:

    A Scnparticleeventblock block to be called every time SceneKit renders a frame. In this block you can modify the properties of particles in the system.

Discussion

By associating a block with one or more particle properties, you can run arbitrary code that modifies those properties when a significant event in the particle simulation occurs for one or more particles. For example, you can use the following code with a confetti effect to randomly switch between two distinct colors for each spawned particle:

[system handleEvent:SCNParticleEventBirth
      forProperties:@[SCNParticlePropertyColor]
          withBlock:^(void **data, size_t *dataStride, uint32_t *indices , NSInteger count) {
              for (NSInteger i = 0; i < count; ++i) {
                  float *color = (float *)((char *)data[0] + dataStride[0] * i);
                  if (rand() & 0x1) { // Switch the green and red color components.
                      color[0] = color[1];
                      color[1] = 0;
                  }
              }
          }];

See Also

Modifying Particles in Response to Particle System Events