【问题标题】:Drawing a shape in drawRect removes gradient在 drawRect 中绘制形状会移除渐变
【发布时间】:2013-02-25 20:52:03
【问题描述】:

我对核心图形非常陌生。

基本上我有一个绘制渐变背景的视图,我试图用另一个视图创建这个视图的子类,然后在这个子类中绘制一个三角形。

发生的情况是没有绘制渐变。我怀疑是因为我的子类在 drawRect 中绘制了一个三角形(超级绘制它的渐变),我的子类覆盖了 drawRect 方法并且超级被忽略了。

如何同时绘制渐变和三角形?

这是我的子类 drawRect 的代码

- (void)drawRect:(CGRect)rect
{
     // get the current content
CGContextRef context = UIGraphicsGetCurrentContext();

CGFloat buttonHeight = self.bounds.size.height;
CGFloat buttonWidth = self.bounds.size.width;

// draw triangle
UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(buttonWidth / 2, buttonHeight * .75)];
[path addLineToPoint:CGPointMake(buttonWidth * .75, buttonHeight * .25)];
[path addLineToPoint:CGPointMake(buttonWidth * .25,buttonHeight * .25)];

CGContextAddPath(context, path.CGPath);
CGContextClip(context);
[_arrowColor set];
[path fill];
}

【问题讨论】:

    标签: iphone ios objective-c xcode ipad


    【解决方案1】:

    如果您在子类的 drawRect: 开头调用 [super drawRect:rect];,那么它将首先绘制渐变,然后是三角形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多