gpt4 book ai didi

ios - 如何裁剪带有重复半圆的 UIView?

转载 作者:搜寻专家 更新时间:2023-11-01 06:15:56 25 4
gpt4 key购买 nike

我想像这张图片那样裁剪一个底部和顶部重复半圆的 UIView enter image description here

最佳答案

我一直在研究你的问题,这是我的结果,你需要创建一个 UIBezierPath 并应用到你想要的 View ,为此使用此代码

生成所需 BezierPath 的函数

func pathSemiCirclesPathForView(givenView: UIView, ciclesRadius:CGFloat = 10, circlesDistance : CGFloat = 2) ->UIBezierPath
{
let width = givenView.frame.size.width
let height = givenView.frame.size.height

let semiCircleWidth = CGFloat(ciclesRadius*2)

let semiCirclesPath = UIBezierPath()
semiCirclesPath.move(to: CGPoint(x:0, y:0))

var x = CGFloat(0)
var i = 0
while x < width {
x = (semiCircleWidth) * CGFloat(i) + (circlesDistance * CGFloat(i))
let pivotPoint = CGPoint(x: x + semiCircleWidth/2, y: height)
semiCirclesPath.addArc(withCenter: pivotPoint, radius: ciclesRadius, startAngle: -180 * .pi / 180.0, endAngle: 0 * .pi / 180.0, clockwise: true)
semiCirclesPath.addLine(to: CGPoint(x: semiCirclesPath.currentPoint.x + circlesDistance, y: height))
i += 1
}

semiCirclesPath.addLine(to: CGPoint(x:width,y: 0))

i = 0
while x > 0 {
x = width - (semiCircleWidth) * CGFloat(i) - (circlesDistance * CGFloat(i))
let pivotPoint = CGPoint(x: x - semiCircleWidth/2, y: 0)
semiCirclesPath.addArc(withCenter: pivotPoint, radius: ciclesRadius, startAngle: 0 * .pi / 180.0, endAngle: -180 * .pi / 180.0, clockwise: true)
semiCirclesPath.addLine(to: CGPoint(x: semiCirclesPath.currentPoint.x - circlesDistance, y: 0))
i += 1
}

semiCirclesPath.close()
return semiCirclesPath
}

将 BezierPath 应用于任何 View 的函数

func applySemiCircleEffect(givenView: UIView){
let shapeLayer = CAShapeLayer(layer: givenView.layer)
shapeLayer.path = self.pathSemiCirclesPathForView(givenView: givenView).cgPath
shapeLayer.frame = givenView.bounds
shapeLayer.masksToBounds = true
shapeLayer.shadowOpacity = 1
shapeLayer.shadowColor = UIColor.black.cgColor
shapeLayer.shadowOffset = CGSize(width: 0, height: 0)
shapeLayer.shadowRadius = 3

givenView.layer.mask = shapeLayer
}

使用它

@IBOutlet weak var customView: UIView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.applySemiCircleEffect(givenView: customView)
}

这是它的样子

enter image description here

希望这对你有帮助,祝你编程愉快

关于ios - 如何裁剪带有重复半圆的 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45115304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com