【问题标题】:CGContextClip in drawRect is not workingdrawRect 中的 CGContextClip 不起作用
【发布时间】:2014-03-31 09:54:41
【问题描述】:

我一直在尝试剪切视图以使其指向右侧,但由于某种原因,它不起作用。

以下是我在drawRect中的代码:

CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextMoveToPoint(ctx, width - 20.0, 0.0);
CGContextAddLineToPoint(ctx, width, height / 2.0);
CGContextAddLineToPoint(ctx, width - 20.0, height);

[[UIColor blackColor] setStroke];
CGContextStrokePath(ctx);

CGContextClip(ctx);

我只是把描边路径放上来看看它是否有效。 笔画工作正常,但 CGContextClip() 无法挽救我的生命(没有笔画功能)。

请帮帮我!!提前致谢!

【问题讨论】:

  • 你想要像 traingle 这样的东西吗?
  • 是的,我正在尝试制作一个具有指向右侧的矩形,就像那些允许您在向下钻取界面中导航的东西一样
  • 因此您可以使用CGContext 简单地绘制一个矩形。你为什么要剪掉路径?
  • 哦.......我是编程新手,所以我没有意识到。你的意思是,drawRect 是绘制矩形的地方,所以我应该继续用我的配置绘制矩形吗?谢谢!

标签: ios core-graphics drawrect cgcontext


【解决方案1】:

使用CoreGraphics 制作一个三角形

-(void) drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect)/2, CGRectGetMaxY(rect));
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMinY(rect));
    [[UIColor redColor] setFill];
    CGContextFillPath(ctx);
}

【讨论】:

  • 谢谢。我从你的评论中得到了提示,所以我把它整理出来了:)非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
相关资源
最近更新 更多