将 UIImageView 的区域涂黑或显示部分视图

发布于 2024-11-08 01:21:17 字数 148 浏览 3 评论 0原文

有没有办法在不创建覆盖的 UIView 实例(使用此框架)并将其涂黑的情况下涂黑 UIImageView 的区域(由框架定义)?

是否有类似的方法可以显示 UIImageView 的一部分,而不使用 UIView 实例来遮蔽图像的其余部分?

Is there a way to black out a region (defined by a frame) of a UIImageView without creating an overlaying UIView instance (with this frame) and blacking that out?

Is there similarly a way to reveal part of a UIImageView without using UIView instances to black out the rest of the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装爱人 2024-11-15 01:21:17

我将在这里作弊并建议您使用图层,因为它们重量轻。

CALayer *blackLayer = [CALayer layer];
blackLayer.backgroundColor = [UIColor blackColor];
blackLayer.frame = imageView.bounds;
[imageView.layer addSublayer:blackLayer];

对于第二部分,您可以考虑使用图层网格(黑色)。当用户触摸图像视图时,您可以从他触摸的区域中选取图层并将它们从超级图层(即图像视图的根图层)中删除。

I am going to cheat a bit here and suggest that you use layer as they are light weight.

CALayer *blackLayer = [CALayer layer];
blackLayer.backgroundColor = [UIColor blackColor];
blackLayer.frame = imageView.bounds;
[imageView.layer addSublayer:blackLayer];

For the second part you can consider using a grid of layers (black). When user touches the image view, you can pick the layers from the area he has touched and remove them from the super layer i.e. the image view's root layer.

顾忌 2024-11-15 01:21:17

您可以对 UIIMageView 进行子类化,并根据您的需要在 drawRectMethod 中进行绘制。

  1. 您可以创建 UIIMageView 的子类并在其中定义 drawRectMethod 。
  2. 您还可以通过在视图上调用 setNeedsLayout 方法来随意调用drawRectMethod。(setNeedsDisplay - 导致drawRect被调用 - 。只有contentMode = UIViewContentModeRedraw才会导致当视图的边界发生变化时调用setNeedsDisplay)。

更新:

http://developer.apple.com/库/ios/#documentation/CoreGraphics/Reference/CoreGraphics_Framework/_index.html

you can subclass the UIIMageView and draw as per your needs in the drawRectMethod.

  1. You can create a subclass of UIIMageView and have your definition of drawRectMethod over there.
  2. You can also invoke the drawRectMethod at your will by calling setNeedsLayout method on the view .( setNeedsDisplay - causes drawRect to be called - . Only contentMode = UIViewContentModeRedraw will cause setNeedsDisplay to be called when a view's bounds changes).

UPDATE:

http://developer.apple.com/library/ios/#documentation/CoreGraphics/Reference/CoreGraphics_Framework/_index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文