【问题标题】:Core Graphics state management核心图形状态管理
【发布时间】:2010-10-27 19:06:19
【问题描述】:

在一些代码中sn-p

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]);

  UIGraphicsPushContext(ctx);
  ...

设置当前填充颜色,然后将状态推送到堆栈。 其他sn-p:

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  UIGraphicsPushContext(ctx);

  [[UIColor darkTextColor] set];
  ...

哪种方式是正确的?这两种方法CGContextSetFillColorWithColorUIColor set 在状态管理方面有什么区别?

【问题讨论】:

    标签: iphone core-graphics


    【解决方案1】:

    唯一的区别是当你使用 UIColor 方法时你不能指定更新哪个上下文,它总是更新当前上下文。 UIGraphicsPushContext 将一个新的上下文设置为当前上下文,因此顺序当然很重要。 (另一个小区别是 set 方法同时设置了填充颜色和描边颜色)。

    否则没有区别,你可以使用任何你喜欢的。

    在您的示例中,我可能会使用 CGContextSetFillColorWithColor,因为在这种情况下您不必使用 UIGraphicsPushContext 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多