如何屏蔽 UIView [英] How to mask a UIView

查看:32
本文介绍了如何屏蔽 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,在该应用程序中我通过代码绘制 UIView 并用 UIColor 填充它.接下来我想加载一个 mask.png 文件(没有透明度,只有黑白)并屏蔽 UIView 以改变其视觉外观.

I'm working on an app where I draw a UIView via code and I fill it with a UIColor. Next thing I want to load a mask.png file (no transparency, just black and white) and mask the UIView to change its visual appearance.

知道怎么做吗?

推荐答案

// Create your mask layer
CALayer* maskLayer = [CALayer layer];
maskLayer.frame = CGRectMake(0,0,yourMaskWidth ,yourMaskHeight);
maskLayer.contents = (__bridge id)[[UIImage imageNamed:@"yourMaskImage.png"] CGImage];

// Apply the mask to your uiview layer        
yourUIView.layer.mask = maskLayer;

记得导入 QuartzCore 并添加框架

Remember to import QuartzCore and add the framework

#import <QuartzCore/QuartzCore.h>

这很容易,但我在任何地方都没有找到答案!

It is very easy but I didn't find the answer anywhere!

这篇关于如何屏蔽 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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