除了swift中的假边框外,一切都清楚了

时间:2017-03-09 15:53:54

标签: ios swift

我正在使用具有背景颜色的视图实现“假”边框,以便边框不会覆盖另一个视图。 (根据this answer和以下代码)

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
backgroundView.backgroundColor = [UIColor blackColor]; /* I want this to be clear except for the part outside bView */
backgroundView.clipsToBounds = NO;

UIView *bView = [[UIView alloc] initWithFrame:CGRectInset(backgroundView.bounds, 3, 3)];
bView.backgroundColor = [UIColor redColor];  /* I want this to be clear */

UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(-50, -50, 100, 100)];
cView.backgroundColor = [UIColor yellowColor];
[bView addSubview:cView];

[backgroundView addSubview:bView];

[self.window addSubview:backgroundView];

除边框和backgroundView完全透明外,如何bview清晰(透明)?如果我将两种颜色设置为清除,我将失去边界。我使用swift不像代码示例。

由于

这就是我真正想要的。大盒子需要透明,除了它周围的假黑色边框,以便文本(及其背后的所有内容)都能显示出来。

enter image description here

2 个答案:

答案 0 :(得分:1)

好吧,我不能给你明确的性能数据,但我希望这会给你提供比多个视图更好的性能来创建一个“假的”边框......

class myView: UIView {

    override func draw(_ rect: CGRect) {

        UIColor.black.set()

        let context = UIGraphicsGetCurrentContext()
        context?.stroke(rect.insetBy(dx: 1.5, dy: 1.5), width: 3.0)

    }

}

let backgroundView = myView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
backgroundView.backgroundColor = UIColor.clear

let cView = UIView(frame: CGRect(x: -50, y: -50, width: 100, height: 100))
cView.backgroundColor = UIColor.yellow

backgroundView.addSubview(cView)

self.view.addSubview(backgroundView)

当然,如果你真的希望用子视图来创建框架,那么这也可以完成这项工作。它为背景视图添加了4个子视图,矩形的每一边都有一个:

let backgroundView = UIView(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
backgroundView.backgroundColor = UIColor.clear
backgroundView.clipsToBounds = false

let bgvFrame = backgroundView.bounds

let leftEdge = UIView(frame: CGRect(x: 0, y: 0, width: 3, height: bgvFrame.size.height))
leftEdge.backgroundColor = UIColor.black

let topEdge = UIView(frame: CGRect(x: 0, y: 0, width: bgvFrame.size.width, height: 3))
topEdge.backgroundColor = UIColor.black

let rightEdge = UIView(frame: CGRect(x: bgvFrame.size.width - 3, y: 0, width: 3, height: bgvFrame.size.height))
rightEdge.backgroundColor = UIColor.black

let bottomEdge = UIView(frame: CGRect(x: 0, y: bgvFrame.size.height - 3, width: bgvFrame.size.width, height: 3))
bottomEdge.backgroundColor = UIColor.black

backgroundView.addSubview(leftEdge)
backgroundView.addSubview(topEdge)
backgroundView.addSubview(rightEdge)
backgroundView.addSubview(bottomEdge)

let cView = UIView(frame: CGRect(x: -50, y: -50, width: 100, height: 100))
cView.backgroundColor = UIColor.yellow

backgroundView.addSubview(cView)

self.view.addSubview(backgroundView)

答案 1 :(得分:1)

你说你想要这个:

enter image description here

所以现在我将很容易地构建它,但我会将第三个视图设为白色,以便我们可以看到它(带有应该清楚的注释):

    self.view.backgroundColor = .gray

    let borderView = UIView(frame:CGRect(x: 150, y: 150, width: 200, height: 200))
    borderView.backgroundColor = .clear
    borderView.layer.borderWidth = 3
    self.view.addSubview(borderView)

    let yellowView = UIView(frame:CGRect(x: 100, y: 100, width: 100, height: 100))
    yellowView.backgroundColor = .yellow
    self.view.addSubview(yellowView)

    let clearView = UIView(frame:borderView.frame.insetBy(dx: 3, dy: 3))
    clearView.backgroundColor = .white // should be .clear
    self.view.addSubview(clearView)

结果:

enter image description here

.clear替换为.white以获得所需的结果。