UIView上的圆圈层的Swift蒙版 [英] Swift mask of circle layer over UIView

查看:630
本文介绍了UIView上的圆圈层的Swift蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用swift中的圆形CAShapeLayer掩盖方形UIView。我有以下内容:

I am attempting to mask a square UIView by a circular CAShapeLayer in swift. I have the following:

    var snapFrame = self.snapButton.frame
    var innerFrame = CGRect(x: snapFrame.minX + 1, y: snapFrame.minY + 1, width: snapFrame.width - 2, height: snapFrame.height - 2)

    maskLayer = CAShapeLayer()
    var circlePath = UIBezierPath(roundedRect: innerFrame, cornerRadius: innerFrame.width)
    maskLayer.path = circlePath.CGPath
    maskLayer.fillColor = UIColor.clearColor().CGColor

    shutterOverlay = UIView()
    shutterOverlay.frame = innerFrame
    shutterOverlay.backgroundColor = BUBConstants.primaryColor_blue

    self.view.addSubview(shutterOverlay)
    self.view.layer.addSublayer(maskLayer)

    shutterOverlay.layer.mask = maskLayer

如果我评论最后两个线条,图层和视图都显示在正确的位置和正确的大小。但是,添加最后一行会导致视图和图层都不显示。

If I comment out the last two lines, both the layer and the view appear in the correct places and at the correct sizes. However, adding the last line causes both the view and layer to not be shown.

另外,我需要这样做,因为我的最终目标是制作一个动画,其中方形UIView填充圆圈。我不能只显示一个圆形视图。

Also, I need to do it this way as my end goal is to have an animation where the square UIView fills up the circle. I can't just show a circular view.

有人能指出我哪里出错吗?

Can anyone point me to where I am going wrong?

推荐答案

你需要像这样把面具添加到shutterOverlay

You need to add the mask to the shutterOverlay like this

shutterOverlay.layer.addSublayer(maskLayer)

而不是视图的图层。

面具需要是它要掩盖的图层的子图层。

The mask needs to be a sublayer of the layer it wants to mask out.

这篇关于UIView上的圆圈层的Swift蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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