-1

我正在尝试下面的代码来绘制形状,这没关系,但是UIView 背景颜色没有显示,因为我给View赋予了橙色

查看我的代码:

let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
myView.backgroundColor = .orange
let circlePath = UIBezierPath(arcCenter: CGPoint(x: myView.bounds.size.width / 2, y: 0), radius: myView.bounds.size.height, startAngle: 0.0, endAngle: .pi, clockwise: false)
let circleShape = CAShapeLayer()
circleShape.masksToBounds = false
circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.orange.cgColor
circleShape.strokeColor = UIColor.orange.cgColor
myView.layer.mask = circleShape
print(myView)

操场上的输出:

截屏

4

4 回答 4

1

试试这个代码:

    let myView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    myView.backgroundColor = .orange
    let circlePath = UIBezierPath(arcCenter: myView.center,
                                  radius: myView.bounds.size.width / 2,
                                  startAngle: 0.0,
                                  endAngle: .pi,
                                  clockwise: false)
    let circleShape = CAShapeLayer()
    circleShape.path = circlePath.cgPath
    myView.layer.mask = circleShape

我认为由于错误的 arcCenter 或起点和终点天使,您看不到预期的结果

代码结果: 在此处输入图像描述

于 2020-02-13T10:43:35.940 回答
0

只需在绘图后到处删除 path.move(to:) 调用。这帮助我解决了同样的问题

于 2020-07-25T23:23:34.127 回答
0

问题似乎与您的circlePath, 有:

1) 错误的 arcCenter (CGPoint'sy不居中)

2)半径(它应该是视图的高度/宽度的一半,考虑height = width到你需要一个圆)

3)角度(应该是360度)

将其定义替换为:

let circlePath = UIBezierPath(arcCenter: myView.center, radius: myView.bounds.height/2, startAngle: 0.0, endAngle: .pi * 2, clockwise: false)

它应该按预期工作......

没关系,但是 UIView 背景颜色没有显示,因为我给 View 赋予了橙色

您使用的路径甚至不显示图层,因为您已将其角半径指定为与其高度相同,并且您已将生成的所谓不可见图层应用为视图的蒙版。

于 2020-02-13T10:58:01.793 回答
0

几点观察:

  1. 您只是在查看路径的视觉表示,而不是视图。要查看视图,您应该设置liveView:PlaygroundPage

    import PlaygroundSupport
    

    PlaygroundPage.current.liveView = myView
    PlaygroundPage.current.needsIndefiniteExecution = true
    
  2. 顺便说一句,您的圆形视图从 0 逆时针变为 π。即,这是一个圆圈的上半部分。因此,如果圆的中心有一个yof 0,这意味着您的路径在视图上方并且不与它重叠。将其设置为顺时针方向(以获得圆的下半部分)或y向下移动(使圆的上半部分与视图重叠)。

  3. 我发现游乐场会调整它们的大小liveView(即使我设置wantsFullScreenLiveViewfalse),所以如果我想要特定大小的视图,我会使用“容器视图”。即,我将添加myView为 的子视图containerView,添加约束以将其放在该容器的中心,然后设置containerViewliveView.

  4. 仅相切相关,但您将蒙版形状图层的颜色设置为橙色。蒙版只使用它们的 alpha 通道,所以使用橙色是没有意义的。我们通常将遮罩设置为白色,但我只是想确保我们没有将遮罩的颜色与遮罩视图的颜色混为一谈。

因此,将所有这些放在一起:

import UIKit
import PlaygroundSupport

// create container
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false

// create view that we'll mask with shape layer
let rect = CGRect(x: 0, y: 0, width: 200, height: 200)
let myView = UIView()
myView.translatesAutoresizingMaskIntoConstraints = false
myView.backgroundColor = .orange
myView.clipsToBounds = true
containerView.addSubview(myView)

// create mask
let circlePath = UIBezierPath(arcCenter: CGPoint(x: rect.midX, y: rect.minY), radius: rect.height, startAngle: 0, endAngle: .pi, clockwise: true)
let circleShape = CAShapeLayer()
circleShape.masksToBounds = false
circleShape.path = circlePath.cgPath
circleShape.fillColor = UIColor.white.cgColor
circleShape.strokeColor = UIColor.white.cgColor
myView.layer.mask = circleShape

// center masked view in container
NSLayoutConstraint.activate([
    myView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
    myView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
    myView.widthAnchor.constraint(equalToConstant: 200),
    myView.heightAnchor.constraint(equalToConstant: 200)
])

// show it
PlaygroundPage.current.liveView = containerView
PlaygroundPage.current.needsIndefiniteExecution = true

这会产生:

在此处输入图像描述

因此,通过使弧顺时针方向移动并使用liveView,我们现在可以看到它。显然,圆心为midX, minY且半径为 的圆弧height不能显示完整的半圆。如果您想看到完整的半圆,则必须将半径设置为width / 2(或增加widthof或其他)。rect

于 2020-02-13T17:06:58.150 回答