通过图像遮罩创建粒子
我正在尝试使用 CGContextClipToMask() 函数创建不同颜色的粒子。由于某种原因,我的第一个粒子似乎通过其更新完全显示和动画化,但根本没有其他粒子显示。这是一段代码:
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);
当我注释掉 ClipToMask 调用时,我看到所有各种填充矩形都在动画(即一堆以粒子效果方式填充的正方形。但是,通过调用,我只看到第一个。有吗?我明显失踪了什么?
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 apparent I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据石英文档:
“剪切区域是图形状态的一部分。要将剪切区域恢复到之前的状态,可以在剪切之前保存图形状态,并在完成剪切绘制后恢复图形状态。”
因此,我在 while 循环中围绕我的代码插入了保存和恢复调用,如下所示:
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: