菜鸟教程小白 发表于 2022-12-12 10:11:04

ios - 通过 CGMutablePathRef 为 drawRect 中绘制的框设置动画


                                            <p><p>我的目标是在我的 UIView 上画一个空的圆形框,它应该显示为一个洞,以显示下面的内容。
我通过覆盖 drawRect 方法来做到这一点,如下所示。
这使我能够创建我的 holeRect 属性(它是一个 CGRect)大小的圆角矩形,将内部阴影放入其中并将其放置在我的 View 上,清除(使用 <strong>clearColor</strong> 和 < strong>CGContextSetBlendMode(context, kCGBlendModeSourceOut)</strong> ) 这个框覆盖的区域并揭示了我的观点背后的内容(我从 <a href="https://stackoverflow.com/questions/4431292/inner-shadow-effect-on-uiview-layer" rel="noreferrer noopener nofollow">this</a> 问题中获取了一些代码并对其进行了修改)。
现在我的问题是我必须用动画移动和调整这个矩形的大小,我找不到这样做的方法,也许我选择了错误的方法来做这件事,但我不是很擅长绘画所以任何提示将不胜感激。</p>

<pre><code>- (void)drawRect:(CGRect)rect
{



//I set the frame of my &#34;holey&#34; rect
CGRect bounds =self.holeRect;
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat radius = 20;

//fill my whole view with gray
CGContextSetFillColorWithColor( context, .CGColor );
CGContextFillRect( context, rect );

// Create the &#34;visible&#34; path, which will be the shape that gets the inner shadow
// In this case it&#39;s just a rounded rect, but could be as complex as your want
CGMutablePathRef visiblePath = CGPathCreateMutable();
CGRect innerRect = CGRectInset(bounds, radius, radius);
CGPathMoveToPoint(visiblePath, NULL, innerRect.origin.x, bounds.origin.y);
CGPathAddLineToPoint(visiblePath, NULL, innerRect.origin.x + innerRect.size.width, bounds.origin.y);
CGPathAddArcToPoint(visiblePath, NULL, bounds.origin.x + bounds.size.width, bounds.origin.y, bounds.origin.x + bounds.size.width, innerRect.origin.y, radius);
CGPathAddLineToPoint(visiblePath, NULL, bounds.origin.x + bounds.size.width, innerRect.origin.y + innerRect.size.height);
CGPathAddArcToPoint(visiblePath, NULL,bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height, innerRect.origin.x + innerRect.size.width, bounds.origin.y + bounds.size.height, radius);
CGPathAddLineToPoint(visiblePath, NULL, innerRect.origin.x, bounds.origin.y + bounds.size.height);
CGPathAddArcToPoint(visiblePath, NULL,bounds.origin.x, bounds.origin.y + bounds.size.height, bounds.origin.x, innerRect.origin.y + innerRect.size.height, radius);
CGPathAddLineToPoint(visiblePath, NULL, bounds.origin.x, innerRect.origin.y);
CGPathAddArcToPoint(visiblePath, NULL,bounds.origin.x, bounds.origin.y, innerRect.origin.x, bounds.origin.y, radius);
CGPathCloseSubpath(visiblePath);

// Fill this path
UIColor *aColor = ;
;
CGContextAddPath(context, visiblePath);
CGContextSetBlendMode(context, kCGBlendModeSourceOut);
CGContextFillPath(context);


// Now create a larger rectangle, which we&#39;re going to subtract the visible path from
// and apply a shadow
CGMutablePathRef path = CGPathCreateMutable();
//(when drawing the shadow for a path whichs bounding box is not known pass &#34;CGPathGetPathBoundingBox(visiblePath)&#34; instead of &#34;bounds&#34; in the following line:)
//-42 cuould just be any offset &gt; 0
CGPathAddRect(path, NULL, CGRectInset(bounds, -42, -42));

// Add the visible path (so that it gets subtracted for the shadow)
CGPathAddPath(path, NULL, visiblePath);
CGPathCloseSubpath(path);

// Add the visible paths as the clipping path to the context
CGContextAddPath(context, visiblePath);
CGContextClip(context);


// Now setup the shadow properties on the context
aColor = ;
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 5.0f, );

// Now fill the rectangle, so the shadow gets drawn
;   
CGContextSaveGState(context);   
CGContextAddPath(context, path);
CGContextEOFillPath(context);

// Release the paths
//CGPathRelease(path);
CGPathRelease(visiblePath);

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>看看我对一个关于在 UIImageView 中打洞的问题给出的答案:</p>

<p> <a href="https://stackoverflow.com/a/8632731/341994" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/8632731/341994</a> </p>

<p>请注意,它的工作方式是 UIImageView 的 superView 层有一个子层,它进行 mask 并因此打洞。这意味着要为孔设置动画,您所要做的就是为该子层的移动设置动画。正如我的书中所述,Core Animation 很简单:</p>

<p> <a href="http://www.apeth.com/iOSBook/ch17.html#_using_a_cabasicanimation" rel="noreferrer noopener nofollow">http://www.apeth.com/iOSBook/ch17.html#_using_a_cabasicanimation</a> </p>

<p>换句话说,如果你可以将你正在做的所有事情都封装为一个层,那么通过移动层来为它制作动画是微不足道的。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 通过 CGMutablePathRef 为 drawRect 中绘制的框设置动画,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15888619/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15888619/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 通过 CGMutablePathRef 为 drawRect 中绘制的框设置动画