使用图层蒙版使 UIView 的部分透明? [英] Use Layer Mask to make parts of the UIView transparent?

查看:48
本文介绍了使用图层蒙版使 UIView 的部分透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController,其 self.view 的背景颜色为红色.我想在屏幕中央放置一个 UIImageView.UIImageView 显示覆盖整个屏幕,其背景颜色应为蓝色.当用作遮罩时,中心应该有一个 100 x 100 的图像.该图像是具有透明和非透明部分的 png.使用此图像作为遮罩,背景应该可以通过此 png 的非透明部分可见.它应该是这样的:

我该怎么做?

解决方案

你能分享你的图片吗?

查看演示代码后.你的方法不会像以前一样工作.他的背景是一张图片.

尝试以下操作:

1- 在您的 App Delegate 中,将窗口背景颜色设为蓝色.

self.window?.backgroundColor = UIColor.blueColor()

将您的代码替换为以下内容:

self.view.backgroundColor = UIColor.redColor()让 maskImage: UIImage = UIImage(named: "twitterIcon")!掩码 = CALayer()mask?.contents = maskImage.CGImagemask?.contentsGravity = kCAGravityResizeAspectmask?.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)掩码?.anchorPoint = CGPoint(x: 0.5, y: 0.5)mask?.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height/2)self.view.layer.mask = 面具!

I have a UIViewController with a background color red of its self.view. I want to place a UIImageView in the center of the screen. The UIImageView show cover the entire screen and its background color should be blue. There should be a 100 x 100 image in the center when acts as mask. The image is a png with transparent and non transparent parts. Using this image as mask the background should be visible through the non transparent parts of this png. It should look like this:

Here is a demo how this looks in an application.

Here is what I did:

self.view.backgroundColor = UIColor.redColor()
let imageView = UIImageView(frame: self.view.frame)
imageView.backgroundColor = UIColor.blueColor()
let maskImage: UIImage = UIImage(named: "twitterIcon")!
mask = CALayer()
mask?.contents = maskImage.CGImage
mask?.contentsGravity = kCAGravityResizeAspect
mask?.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
mask?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
mask?.position = CGPoint(x: imageView.frame.size.width/2, y: imageView.frame.size.height/2)
imageView.layer.mask = mask!
self.imageView = imageView
self.view.addSubview(imageView)

And here is the result:

I want to achieve exactly the opposite. The image should be in the center of size 100 x 100. The non transparent part of the image should be the mask which should the view behind (red in this case). The rest of the screen should be covered with blue.

Edit: Here is the image I use.

How can I do that?

解决方案

Can you share your image?

EDIT:

After looking at the demo code. Your approach will not work as the same. His background is an image.

Try the following:

1- In your App Delegate make the window background colour blue.

self.window?.backgroundColor = UIColor.blueColor()

Replace your code with the following:

self.view.backgroundColor = UIColor.redColor()
    let maskImage: UIImage = UIImage(named: "twitterIcon")!
    mask = CALayer()
    mask?.contents = maskImage.CGImage
    mask?.contentsGravity = kCAGravityResizeAspect
    mask?.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
    mask?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    mask?.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height/2)
    self.view.layer.mask = mask!

这篇关于使用图层蒙版使 UIView 的部分透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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