gpt4 book ai didi

ios - UIView如何绘制相机选择器

转载 作者:行者123 更新时间:2023-11-28 20:51:44 24 4
gpt4 key购买 nike

我正在寻找在 View 周围绘制此类框架的方法。是否可以使用 iOS 原生绘图? enter image description here

最佳答案

使用原生渲染很容易绘制它。一种方法是创建一个新 View 并覆盖 draw rect 方法。您可以执行以下操作:

class ViewWithFrame: UIView {

var color: UIColor = UIColor.black { didSet { setNeedsDisplay() } }
var lineThickness: CGFloat = 5 { didSet { setNeedsDisplay() } }
var lineLength: CGFloat = 30.0 { didSet { setNeedsDisplay() } }

override var frame: CGRect { didSet { setNeedsDisplay() } }
override func layoutSubviews() {
super.layoutSubviews()
setNeedsDisplay()
}

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

var paths: [UIBezierPath] = [UIBezierPath]()

// Top left
paths.append({
let path = UIBezierPath()
path.move(to: .zero)
path.addLine(to: CGPoint(x: lineLength, y: 0.0))
path.addQuadCurve(to: CGPoint(x: lineLength-lineThickness, y: lineThickness), controlPoint: CGPoint(x: lineLength, y: lineThickness))
path.addLine(to: CGPoint(x: lineThickness, y: lineThickness))
path.addLine(to: CGPoint(x: lineThickness, y: lineLength-lineThickness))
path.addQuadCurve(to: CGPoint(x: 0.0, y: lineLength), controlPoint: CGPoint(x: lineThickness, y: lineLength))
path.close()
return path
}())

// Top right
paths.append({
let path = UIBezierPath()
path.move(to: CGPoint(x: frame.width, y: 0.0))
path.addLine(to: CGPoint(x: frame.width-lineLength, y: 0.0))
path.addQuadCurve(to: CGPoint(x: frame.width-(lineLength-lineThickness), y: lineThickness), controlPoint: CGPoint(x: frame.width-lineLength, y: lineThickness))
path.addLine(to: CGPoint(x: frame.width-lineThickness, y: lineThickness))
path.addLine(to: CGPoint(x: frame.width-lineThickness, y: lineLength-lineThickness))
path.addQuadCurve(to: CGPoint(x: frame.width, y: lineLength), controlPoint: CGPoint(x: frame.width-lineThickness, y: lineLength))
path.close()
return path
}())

// TODO: add 2 bottom paths

color.setFill()
paths.forEach { $0.fill() }
}

}

底部部分仍然需要绘制,但我希望你知道如何绘制。

注意对 setNeedsDisplay 的所有调用。这就是将在下一个运行循环中触发 draw rect 方法的方法,它应该针对可能影响绘图(包括 View 大小)的每个 View 更改执行此操作。

关于ios - UIView如何绘制相机选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57705616/

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