具有 3D 旋转的 CAEmitterCell

CAEmitterCell with 3D rotation

我正在尝试使用 CAEmitterLayer 和 CAEmitterCell 重现从顶部效果掉落的小纸片。

到目前为止,我得到了它的二维动画,但是我很难让每个单元格在下落时旋转。

如何对每个粒子应用随机旋转?到目前为止,我尝试使用 3D Transform 但没有成功。

这是我得到的:

-(void) configureEmitterLayer {
    self.emitterLayer = [CAEmitterLayer layer];
    self.emitterLayer.emitterPosition =  CGPointMake(self.backgroundContainer.bounds.size.width /2, 0);
    self.emitterLayer.emitterZPosition = 10;
    self.emitterLayer.emitterSize = self.backgroundContainer.bounds.size;
    self.emitterLayer.emitterShape = kCAEmitterLayerLine;

    CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];

    emitterCell.contents = (__bridge id)([UIImage imageWithColor:[UIColor whiteColor]].CGImage);

    emitterCell.lifetime = CGFLOAT_MAX;
    emitterCell.lifetimeRange = 4.0;
    emitterCell.birthRate = 2.5;

    emitterCell.color = [[UIColor colorWithRed:1.0f green:1.0 blue:1.0 alpha:1.0] CGColor];
    emitterCell.redRange = 1.0;
    emitterCell.blueRange = 1.0;
    emitterCell.greenRange = 1.0;
    emitterCell.alphaRange = 0.3;

    emitterCell.velocity = 10;
    emitterCell.velocityRange = 3;
    emitterCell.emissionRange = (CGFloat) M_PI_2;
    emitterCell.emissionLongitude = (CGFloat) M_PI;
    emitterCell.yAcceleration = 1;
    emitterCell.zAcceleration = 4;

    emitterCell.spinRange = 2.0;
    emitterCell.scale = 7.0;
    emitterCell.scaleRange = 4.0;

    self.emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell];

    [self.backgroundContainer.layer addSublayer:self.emitterLayer];
}

此库不使用发射器框架,但值得一看以获取灵感。 UIDynamics 附加到单个对象以达到预期效果。

iOS confetti example