//使用百分比画横线
func setHLine(pos:CGFloat) {
let rLinePos:CGFloat = pos * UIScreen.main.bounds.height
let line: UIView = UIView(frame: CGRect(x: 0, y: Int(rLinePos), width: Int(UIScreen.main.bounds.width), height: 1))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
}
//使用百分比画竖线
func setVLine(pos:CGFloat) {
let rLinePos:CGFloat = pos * UIScreen.main.bounds.width
let line: UIView = UIView(frame: CGRect(x: Int(rLinePos), y: 0, width: 1, height: Int(UIScreen.main.bounds.height)))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
}
//使用像素画横线
func setHLineByPixel(pos:Int){
let line: UIView = UIView(frame: CGRect(x: 0, y: pos, width: Int(UIScreen.main.bounds.width), height: 1))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
}
//使用像素画竖线
func setVLineByPixel(pos:Int){
let line: UIView = UIView(frame: CGRect(x: pos, y: 0, width: 1, height: Int(UIScreen.main.bounds.height)))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
func setHLine(pos:CGFloat) {
let rLinePos:CGFloat = pos * UIScreen.main.bounds.height
let line: UIView = UIView(frame: CGRect(x: 0, y: Int(rLinePos), width: Int(UIScreen.main.bounds.width), height: 1))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
}
//使用百分比画竖线
func setVLine(pos:CGFloat) {
let rLinePos:CGFloat = pos * UIScreen.main.bounds.width
let line: UIView = UIView(frame: CGRect(x: Int(rLinePos), y: 0, width: 1, height: Int(UIScreen.main.bounds.height)))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
}
//使用像素画横线
func setHLineByPixel(pos:Int){
let line: UIView = UIView(frame: CGRect(x: 0, y: pos, width: Int(UIScreen.main.bounds.width), height: 1))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
}
//使用像素画竖线
func setVLineByPixel(pos:Int){
let line: UIView = UIView(frame: CGRect(x: pos, y: 0, width: 1, height: Int(UIScreen.main.bounds.height)))
line.backgroundColor = UIColor.gray
self.view.addSubview(line)
}
使用方法:
override func viewDidLoad() {
super.viewDidLoad()
setHLine(pos: 0.056)
setHLine(pos: 0.29)
setHLine(pos: 0.38)
setVLine(pos: 0.3)
setHLineByPixel(pos: 10)
setVLineByPixel(pos: 10)
}
本文介绍如何在Swift中使用百分比及像素位置来绘制横线和竖线,包括具体实现方法和示例代码。
242

被折叠的 条评论
为什么被折叠?



