我想在我的画中画出内部阴影。我使用核心图形绘制了一个红色徽章,并希望在徽章边界上有一个内部黑色阴影。请帮助如何实现这一点?
我附上我的代码和徽章图片:
- (void)drawBadgeInRect:(CGRect)iRect {
UIColor *badgeColor = [UIColor colorWithRed:202.0/255.0 green:19.0/255.0 blue:33.0/255.0 alpha:1.0];
// Drawing the badge
CGContextRef aContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(aContext);
CGFloat aBadgeWidth = 16.0;
CGFloat aBadgeHeight = 16.0;
CGFloat aTextWidth = [self.badge.text sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 7.0f;
if (self.badge.text.length > 1) {
aBadgeWidth = aTextWidth;
}
CGFloat aMaxX = CGRectGetMaxX(iRect) - 1.0 + 50;
CGFloat aMinY = CGRectGetMinY(iRect) + 0.5 - 50;
CGFloat aMaxY = aMinY + aBadgeHeight;
CGFloat aMinX = aMaxX - aBadgeWidth;
CGSize myShadowOffset = CGSizeMake (-50, 50);
CGFloat myColorValues[] = {1, 0, 0, 1};
CGColorRef myColor;
CGColorSpaceRef myColorSpace;
myColorSpace = CGColorSpaceCreateDeviceRGB ();
myColor = CGColorCreate (myColorSpace, myColorValues);
CGContextSetShadowWithColor (aContext, myShadowOffset, 1, badgeColor.CGColor);
CGContextBeginPath(aContext);
CGContextMoveToPoint(aContext, aMinX, aMinY);
CGContextAddArcToPoint(aContext, aMinX, aMaxY, aMaxX, aMaxY, 15.0);
CGContextAddLineToPoint(aContext, aMaxX, aMaxY);
CGContextAddLineToPoint(aContext, aMaxX, aMinY);
CGContextAddLineToPoint(aContext, aMaxX - aBadgeWidth, aMinY);
CGContextClosePath(aContext);
CGContextFillPath(aContext);
CGColorRelease (myColor);
CGColorSpaceRelease (myColorSpace);
CGContextRestoreGState(aContext);
}