UIview的特定矩形可以有不同的alpha ..?

Ank*_*ava 9 iphone objective-c ios cgrect

我不是一个在其他部分是半透明的图像视图,在中间是完全透明的..看到这个图像.. 在此输入图像描述

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

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

小智 23

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

- (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);
}
Run Code Online (Sandbox Code Playgroud)

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

您可以编写更改maskRect所需的任何其他逻辑,并在此视图上调用"setNeedsDisplay".