简体   繁体   中英

add a transparent view into a translucent view

I want a view like the picture attached, the full view's background is translucent, and the view in green frame is transparent. I tried addSubview and set subview's alpha but it is not worked.

在此处输入图片说明

You can override your drawRect method. Fill it all with translucent, then fill in the transparent part with clear color:

- (void)drawRect:(CGRect)rect {
    // fill it all with translucent
    [yourTranslucentUIColor setFill];
    UIRectFill( rect );


    CGRect yourMiddleHoleRect = CGRectMake(* calculate your rect here);

    [[UIColor clearColor] setFill];
    UIRectFill( yourMiddleHoleRect );
}

I think this will give you a cleaner result than other options.

You could even draw in those green frame indicators here if you wanted.

我相信实现此目标的唯一方法是在透明视图周围添加4个半透明视图。

first you have to add the translucent view over the background view by CGContextDrawImage . Then use CGContextSetBlendMode to clear the portion.

Refer the below project to achieve this.

https://github.com/akopanev/iOS-Scratch-n-See

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM