How do I (successfully) set a maskView to a UIView?

后端 未结 1 647
旧巷少年郎
旧巷少年郎 2021-01-19 02:58

I\'m making a camera-based app and would like to make a frosted overlay on the camera preview, with a rounded rectangle cut out from the frosted view.

I\'ve been try

1条回答
  •  伪装坚强ぢ
    2021-01-19 03:42

    The UIView api says

    An optional view whose alpha channel is used to mask a view’s content.

    The best way to see this is to use a UIImageView loaded with an RGBA image that sports an alpha channel - a PNG exported with transparency switched on from photoshop will do it. And then resize, with the origin set to 0,0.

    A second way to do it would be to create a UIView, with no alpha i.e. [UIColor clearColor] and add a subview which has an alpha [UIColor whiteColor];

    UIView *mask = [[UIView alloc] initWithFrame:(CGRect){0,0,100,100}];
    mask.backgroundColor = [UIColor clearColor];
    UIView *areaToReveal = [[UIView alloc] initWithFrame:(CGRect){20,20,50,50}];
    areaToReveal.backgroundColor = [UIColor whiteColor];
    [mask addSubview:areaToReveal];
    _myView.maskView = mask;
    

    You could add further UIViews which are translucent by x amount using [UIColor colorFromRed: green: blue: alpha:x];

    0 讨论(0)
提交回复
热议问题