我的目標是將一個空的圓角框(它應該顯示爲一個洞)拖到我的UIView上以顯示下面的內容。 我通過覆蓋drawRect方法做到了這一點,如下所示。 這給我創造我holeRect屬性的大小的圓角的矩形(這是一個的CGRect),下降內側陰影到它,並把它放在我的看法,清除能力(使用clearColor和CGContextSetBlendMode(背景下, kCGBlendModeSourceOut))這個盒子覆蓋的區域,揭示了我的觀點背後(我從this問題中拿了一些代碼並修改了它)。 現在我的問題是,我必須移動和調整動畫的矩形,我找不到這樣做的方式,也許我選擇了錯誤的方式來做到這一點,但我並不擅長繪圖,所以任何提示將非常感激。通過CGMutablePathRef爲在drawRect中繪製的框設置動畫效果
- (void)drawRect:(CGRect)rect
{
//I set the frame of my "holey" rect
CGRect bounds = self.holeRect;
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat radius = 20;
//fill my whole view with gray
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:.8].CGColor);
CGContextFillRect(context, rect);
// Create the "visible" path, which will be the shape that gets the inner shadow
// In this case it'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 = [UIColor clearColor];
[aColor setFill];
CGContextAddPath(context, visiblePath);
CGContextSetBlendMode(context, kCGBlendModeSourceOut);
CGContextFillPath(context);
// Now create a larger rectangle, which we'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 "CGPathGetPathBoundingBox(visiblePath)" instead of "bounds" in the following line:)
//-42 cuould just be any offset > 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 = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 5.0f, [aColor CGColor]);
// Now fill the rectangle, so the shadow gets drawn
[aColor setFill];
CGContextSaveGState(context);
CGContextAddPath(context, path);
CGContextEOFillPath(context);
// Release the paths
//CGPathRelease(path);
CGPathRelease(visiblePath);
}
感謝您的回答,但這不會幫助我,我正在閱讀有關Quartz和CA的文檔,但同時我似乎無法找到一種方法來製作類似於您的教程的代碼, m不使用任何類型的UIImageView等等。 我需要使用我手動繪製的盒子,並以某種方式爲它製作動畫。 – 2013-04-11 11:37:27
我的其他代碼中的圖像視圖是不相關的。遮蔽層可以在*後面的任何*處打孔。所以,您只需製作您的繪圖動畫,以及打孔的面具。 – matt 2013-04-11 14:18:13
你是否明白,你的圓形繪圖動畫是非常容易的?如果沒有,先弄清楚,然後再擔心打孔。也許你可以修改你的問題來描述你正在考慮的動畫。 – matt 2013-04-11 14:20:48