如何设置裁剪矩形或区域 [英] How to set up a clipping rectangle or area

查看:149
本文介绍了如何设置裁剪矩形或区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iOS drawRect中设置,修改和清除一个裁剪矩形,以便将线,文本,图像等的通用CG图纸裁剪到视图的一小部分?

How does one set up, modify, and clear a clipping rectangle inside a iOS drawRect for clipping generic CG drawing of lines, text, images, etc. to a small portion of a view?

是否可以使用由多个矩形和圆形组成的更复杂的裁剪区域?

Is it possible to use a more complex clipping region that is a composite of a bunch of rectangles and circles?

推荐答案

您可以使用任意路径设置剪切区域,而不仅限于矩形。以下是一些实现方法:

You can set a clipping area with arbitrary paths, not restricted to rectangles. The followings are some ways of doing it:


  1. 您可以绘制任意路径并设置剪切区域。例如:

  1. You can draw an arbitrary path and set a clipping area with it. For example:

CGContextBeginPath(context);
//draw a path here
CGContextClosePath(context);
CGContextClip(context);
//following drawing on the context will be clipped


  • 如果要使用蒙版图像设置剪切区域,使用CGContextClipToMask方法。

  • If you want to use a mask image to set a clipping area, use CGContextClipToMask method.

    有关更多信息,请参见Apple的 QuartzDemo示例项目用法。

    See Apple's 'QuartzDemo' sample project for more usages.

    这篇关于如何设置裁剪矩形或区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆