I am trying to create particles of varying color by using the CGContextClipToMask() function. For some reason, my first particle seems to show and animate through its updates fully, but no other particle shows at all. Here is a snippet of code:
while(i < mNextParticleIndex)
{
Particle* p = &mParticles[i];
CGRect drawRect = CGRectMake(
p->mPos.x,
p->mPos.y,
p->mSize,
p->mSize);
// try image masking
CGContextClipToMask(context, drawRect, [mImage CGImage]);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetFillColor(context, CGColorGetComponents([UIColor
colorWithRed:p->mColor.r green:p->mColor.g blue:p->mColor.b alpha:p->mColor.a].CGColor));
CGContextFillRect(context, drawRect);
When I comment out the ClipToMask call, I see all my various fill rects animating (i.e. a bunch of squares filling in particle effect fashion. With the call, however, I only see the first one. Is there something a开发者_开发知识库pparent I'm missing?
According to the Quartz docs: "The clipping area is part of the graphics state. To restore the clipping area to a previous state, you can save the graphics state before you clip, and restore the graphics state after you’re done with clipped drawing."
So, I inserted save and restore calls around my code within the while loop as such:
CGContextSaveGState(context);
CGContextClipToMask(context, drawRect, [mImage CGImage]);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetFillColor(context, CGColorGetComponents([UIColor
colorWithRed:p->mColor.r green:p->mColor.g blue:p->mColor.b alpha:p->mColor.a].CGColor));
CGContextFillRect(context, drawRect);
CGContextRestoreGState(context);
精彩评论