【问题标题】:How to remove arc border in shadowed UIView with rounded / arc path?如何使用圆角/弧形路径删除阴影 UIView 中的弧形边框?
【发布时间】:2018-09-23 12:36:56
【问题描述】:

我有这样的票视图

.

我的方法是我有 2 个视图,1 个是票本身,另一个是阴影。我必须这样做,因为如果我屏蔽视图,它会被剪裁,并且阴影不会出现在工单视图中。

这里是创建工单视图的代码:

let shapeLayer = CAShapeLayer()
shapeLayer.frame = someView.bounds
shapeLayer.path = UIBezierPath(roundedRect: someView.bounds,
                               byRoundingCorners: [UIRectCorner.bottomLeft,UIRectCorner.bottomRight] ,
                               cornerRadii: CGSize(width: 5.0, height: 5.0)).cgPath


let rect = CGRect(x:0, y:0, width:200, height:100)
let cornerRadius:CGFloat = 5
let subPathSideSize:CGFloat = 25

let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
let leftSubPath = UIBezierPath(arcCenter: CGPoint(x: rect.width / 2, y: 0),
                               radius: subPathSideSize / 2, startAngle: .pi, endAngle: .pi * 0, clockwise: false)
leftSubPath.close()

let rightSubPath = UIBezierPath(arcCenter: CGPoint(x: rect.width / 2, y: rect.height),
                                radius: subPathSideSize / 2, startAngle: .pi, endAngle: .pi * 0, clockwise: true)
rightSubPath.close()

path.append(leftSubPath)
path.append(rightSubPath.reversing())

let mask = CAShapeLayer()
mask.frame = shapeLayer.bounds
mask.path = path.cgPath
someView.layer.mask = mask

注意:SomeViewTicketView

这是添加阴影的代码:

let shadowMask = CAShapeLayer()
shadowMask.frame = shadowView.bounds
shadowMask.path = path.cgPath
shadowMask.shadowOpacity = 0.2
shadowMask.shadowRadius = 4
shadowMask.masksToBounds = false
shadowMask.shadowOffset = CGSize(width: 0, height: 2)

shadowView.backgroundColor = UIColor.clear
shadowView.layer.addSublayer(shadowMask)

阴影使圆弧/圆角有这样的边框(用红色圆圈标记)。

这是我的Playground gist

你知道如何去除圆角和圆弧路径中的边框吗?

谢谢。

【问题讨论】:

  • 请在问题中添加相关代码,而不是通过链接
  • 完成@AshleyMills
  • 我已经使用了单独的阴影视图@TamásSengel

标签: swift uibezierpath rounded-corners


【解决方案1】:

你需要在你写的这个块中添加clipToBounds。

let mask = CAShapeLayer()
mask.frame = shapeLayer.bounds
mask.path = path.cgPath
someView.clipsToBounds = true
someView.layer.mask = mask

【讨论】:

    【解决方案2】:

    所以我认为掩码和路径中的角有不同的计算。所以我使用了shadowLayer的fillColor来匹配CouponView的颜色。而现在,边界消失了。

    shadowLayer.fillColor = someView.backgroundColor.cgColor

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2020-11-10
      • 2023-03-29
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多