CGContextSetStrokeColor在drawLayer:inContext中不起作用:

时间:2014-03-08 03:53:38

标签: ios core-animation calayer quartz-core

我想在UIView里面的CALayer中画一条线。我已经设置了CALayer的委托并实现了drawLayer:inContext:方法。以下是方法:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)theContext
{
  CGContextSetStrokeColorWithColor(theContext, [UIColor blueColor].CGColor);
  CGMutablePathRef thePath = CGPathCreateMutable();
  CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
  CGPathAddCurveToPoint(thePath,
                        NULL,
                        15.f,250.0f,
                        295.0f,250.0f,
                        295.0f,15.0f);
  CGContextBeginPath(theContext);
  CGContextAddPath(theContext, thePath);
  CGContextSetLineWidth(theContext, 5);

  CGContextDrawPath(theContext, kCGPathStroke);
}

这很好用,我能看到一条蓝色的线条。但是,当我尝试使用下面的CGContextSetStrokeColorWithColor更改CGContextSetStrokeColor时。这条线消失了,我无法看到任何东西,为什么会发生这种情况?

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)theContext
{
  CGContextSetStrokeColor(theContext, CGColorGetComponents([UIColor blueColor].CGColor));
  CGMutablePathRef thePath = CGPathCreateMutable();
  CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
  CGPathAddCurveToPoint(thePath,
                        NULL,
                        15.f,250.0f,
                        295.0f,250.0f,
                        295.0f,15.0f);
  CGContextBeginPath(theContext);
  CGContextAddPath(theContext, thePath);
  CGContextSetLineWidth(theContext, 5);

  CGContextDrawPath(theContext, kCGPathStroke);
}

请让我知道第二种方法的问题是什么。

0 个答案:

没有答案