UIview的特定矩形可以有不同的alpha ..? [英] can a particular rect of UIview have different alpha..?

查看:61
本文介绍了UIview的特定矩形可以有不同的alpha ..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个在其他部分半透明且在中间完全透明的imageview ..请看这个图片..

I wan't a imageview which is semi-transparent in other parts and completely transparent in the middle .. see this image..

我已经设法做到了,但这不是最好的方法..我可以只有一个rect内部黄线透明......?

i have managed to do it but it is not the best of ways.. can I only have a rect inside yellow lines transparent ...?

编辑:是否可以更改视图某个矩形的alpha值?

EDIT: Is it possible to change the alpha of a certain rect of a view..?

推荐答案

在视图上放置一个自定义UIView,显示图像,大小和位置,使其完全重叠。
在自定义视图中的drawRect方法

Place a custom UIView over the view showing the image, size and position it to exactly overlap. In the custom view's drawRect method

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGRect rBounds = self.bounds;

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Fill background with 80% white
    CGContextSetFillColorWithColor(context, [[[UIColor whiteColor] colorWithAlphaComponent:0.8] CGColor]);
    CGContextFillRect(context, rBounds);

    // Draw the window 'frame'
    CGContextSetStrokeColorWithColor(context, [[UIColor orangeColor] CGColor]);
    CGContextSetLineWidth(context, 10);
    CGContextStrokeRect(context, self.maskRect);

    // make the window transparent
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextFillRect(context, self.maskRect);
}

其中maskRect是要透明显示的矩形。
确保自定义视图的背景颜色设置为'clearColor'

where maskRect is the rectangle to be shown transparent. Do ensure that the background color of the custom view is set to 'clearColor'

您可以编写更改maskRect和调用'setNeedsDisplay所需的任何其他逻辑'在这个观点上。

You can write any additional logic required to change the maskRect and call 'setNeedsDisplay' on this view.

这篇关于UIview的特定矩形可以有不同的alpha ..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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