Objective c CGContextAddEllipse-重叠get';s夹石英
我喜欢画一些元素的玻璃杯 -顶椭圆 -底部椭圆 -以及中间的线路 接下来,它应该填充一个渐变。这些元素可以工作,但在玻璃的中间与顶部或底部椭圆接触的地方,该区域会被剪裁Objective c CGContextAddEllipse-重叠get';s夹石英,objective-c,core-graphics,quartz-2d,drawrect,cgcontext,Objective C,Core Graphics,Quartz 2d,Drawrect,Cgcontext,我喜欢画一些元素的玻璃杯 -顶椭圆 -底部椭圆 -以及中间的线路 接下来,它应该填充一个渐变。这些元素可以工作,但在玻璃的中间与顶部或底部椭圆接触的地方,该区域会被剪裁 - (void)drawRect:(CGRect)rect { CGPoint c = self.center; // Drawing code CGContextRef cx = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(c
- (void)drawRect:(CGRect)rect
{
CGPoint c = self.center;
// Drawing code
CGContextRef cx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(cx, 1.0);
[[UIColor whiteColor] setStroke];
// DrawTheShapeOfTheGlass
CGContextBeginPath(cx);
// Top and Bottom Ellipse
CGContextAddEllipseInRect(cx, CGRectMake(0, 0, 100, 20));
CGContextAddEllipseInRect(cx, CGRectMake(10, 90, 80, 20));
// Define the points for the Area inbetween
CGPoint points[] = { {0.0,10.0},{10.0,100.0},{90.0,100.0},{100.0,10.0} };
CGContextAddLines(cx, points, 4);
CGContextClosePath(cx);
// Clip, that's only the Clipped-Area wil be filled with the Gradient
CGContextClip(cx);
// CreateAndDraw the Gradient
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat colorSpace[] = {1.0f, 1.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f, 1.0f };
CGFloat locations[] = { 0.0, 1.0 };
CGGradientRef myGradient = CGGradientCreateWithColorComponents(rgbColorSpace, colorSpace, locations, 2);
CGPoint s = CGPointMake(0, 0);
CGPoint e = CGPointMake(100, 100);
CGContextDrawLinearGradient(cx, myGradient, s, e, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGColorSpaceRelease(rgbColorSpace);
CGGradientRelease(myGradient);
}
下面是它的样子:
有没有可能“填满”整个椭圆?我玩混合模式,但没用
谢谢尝试用以下代码替换points[]初始化代码
CGPoint points[] = {{0.0,10.0},{100.0,10.0},{90.0,100.0},{10.0,100.0}};
CoreGraphics使用非零绕组计数规则来确定如何填充路径。由于椭圆是顺时针绘制的,梯形是逆时针绘制的,因此重叠区域没有填充。将梯形的绘图顺序更改为顺时针将导致对象完全填充。尝试用以下代码替换点[]初始化代码
CGPoint points[] = {{0.0,10.0},{100.0,10.0},{90.0,100.0},{10.0,100.0}};
CoreGraphics使用非零绕组计数规则来确定如何填充路径。由于椭圆是顺时针绘制的,梯形是逆时针绘制的,因此重叠区域没有填充。将梯形的绘图顺序更改为顺时针将导致对象完全填充