gpt4 book ai didi

ios - iOS 应用中的形状

转载 作者:可可西里 更新时间:2023-11-01 01:59:56 28 4
gpt4 key购买 nike

当我增加 View 大小时,我正在 UIView 上制作锯齿形,但它无法正常显示。

enter image description here

enter image description here我正在使用这段代码

    func zigZagShape() {
let width = self.frame.size.width
let height = self.frame.size.height
let givenFrame = self.frame

let zigZagWidth = CGFloat(6)
let zigZagHeight = CGFloat(4)

var yInitial = height-zigZagHeight
let zigZagPath = UIBezierPath(rect: givenFrame)
zigZagPath.move(to: CGPoint(x:0, y:0))
zigZagPath.addLine(to: CGPoint(x:0, y:yInitial))

var slope = -1
var x = CGFloat(0)
var i = 0
while x < width {
x = zigZagWidth * CGFloat(i)
let p = zigZagHeight * CGFloat(slope)
let y = yInitial + p
let point = CGPoint(x: x, y: y)
zigZagPath.addLine(to: point)
slope = slope*(-1)
i += 1
}

zigZagPath.addLine(to: CGPoint(x:width,y: 0))
yInitial = 0 + zigZagHeight
x = CGFloat(width)
i = 0
while x > 0 {
x = width - (zigZagWidth * CGFloat(i))
let p = zigZagHeight * CGFloat(slope)
let y = yInitial + p
let point = CGPoint(x: x, y: y)
zigZagPath.addLine(to: point)
slope = slope*(-1)
i += 1
}

let shapeLayer = CAShapeLayer()
shapeLayer.path = zigZagPath.cgPath
self.layer.mask = shapeLayer
}

最佳答案

我已经更正了您的代码,当您使用 UIBezierPath 进行绘图时,不要使用 frame 进行初始化。还有

@IBDesignable
class ZigZiagView: UIView {

override func draw(_ rect: CGRect) {
super.draw(rect)

zigZagShape()
}

func zigZagShape() {

let width = self.frame.size.width
let height = self.frame.size.height
//let givenFrame = self.frame

let zigZagWidth = CGFloat(6)
let zigZagHeight = CGFloat(4)

var yInitial = height-zigZagHeight
let zigZagPath = UIBezierPath()
zigZagPath.move(to: CGPoint(x:0, y:0))
zigZagPath.addLine(to: CGPoint(x:0, y:yInitial))

var slope = -1
var x = CGFloat(0)
var i = 0
while x < width {
x = zigZagWidth * CGFloat(i)
let p = zigZagHeight * CGFloat(slope)
let y = yInitial + p
let point = CGPoint(x: x, y: y)
zigZagPath.addLine(to: point)
slope = slope*(-1)
i += 1
}

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

// draw the line from top right to Bottom
zigZagPath.addLine(to: CGPoint(x:width,y:height))

yInitial = 0 + zigZagHeight
x = CGFloat(width)
i = 0
while x > 0 {
x = width - (zigZagWidth * CGFloat(i))
let p = zigZagHeight * CGFloat(slope)
let y = yInitial + p
let point = CGPoint(x: x, y: y)
zigZagPath.addLine(to: point)
slope = slope*(-1)
i += 1
}

// Now Close the path
zigZagPath.close()

let shapeLayer = CAShapeLayer()
shapeLayer.path = zigZagPath.cgPath
self.layer.mask = shapeLayer
}

}

enter image description here

enter image description here

试试这个,让我知道它是否有效?

关于ios - iOS 应用中的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447682/

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