Ios 从两侧在UIView上绘制对角线
从两侧创建对角线,如swift中的窗格栅 如何以编程方式为视图设计窗栅格(swift4) 下面给出了我的错误逻辑,它画了与视图交叉的一侧对角线Ios 从两侧在UIView上绘制对角线,ios,iphone,swift,swift4,Ios,Iphone,Swift,Swift4,从两侧创建对角线,如swift中的窗格栅 如何以编程方式为视图设计窗栅格(swift4) 下面给出了我的错误逻辑,它画了与视图交叉的一侧对角线 let height = view.frame.size.height let width = view.frame.size.width let space = 10 for i in stride(from: 0, through: 2*Int(width), by: space) { vie
let height = view.frame.size.height
let width = view.frame.size.width
let space = 10
for i in stride(from: 0, through: 2*Int(width), by: space) {
view.layer.addSublayer(DesignShape.addLine(fromPoint: CGPoint(x:
i, y: 0), toPoint: CGPoint(x:CGFloat(i-Int(width)), y: height), color: UIColor.black,lineWidth :2))
}
DesignShape.addLine是使用UIBezierPaths在两点之间画线的方法我用
UIBezierPath
尝试过这种设计。这可能会给你的问题带来一些想法
编码
@IBOutlet weak var shapeView: UIView!
// CONSTRAINTS top 20, left and right 16, height as 320
override func viewDidAppear(_ animated: Bool) {
howManyGrillWeNeed(grillCount : 17, grillWidth: 40, grillHeight : 60)
}
func howManyGrillWeNeed(grillCount: Int, grillWidth: CGFloat, grillHeight: CGFloat)
{
let xPositionDiff = Int((shapeView.frame.width / grillWidth))
var xPosiitonCount : Int = 0
var yPosiitonCount : Int = -1
for i in 0..<grillCount
{
if i % xPositionDiff == 0
{
xPosiitonCount = 0
yPosiitonCount = yPosiitonCount + 1
print("newxLine")
}
else
{
xPosiitonCount = xPosiitonCount + 1
}
let grillVw = UIView(frame: CGRect(x: 0, y: 0, width: grillWidth, height: grillHeight))
grillVw.backgroundColor = UIColor.white
grillVw.frame.origin.x = CGFloat(xPosiitonCount) * grillWidth
grillVw.frame.origin.y = CGFloat(yPosiitonCount) * grillHeight
let layerWidth = grillWidth
let layerHeight = grillHeight
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: layerHeight / 2))
bezierPath.addLine(to: CGPoint(x: layerWidth / 2, y: 0))
bezierPath.addLine(to: CGPoint(x: layerWidth, y: layerHeight / 2))
bezierPath.addLine(to: CGPoint(x: layerWidth / 2, y: layerHeight))
bezierPath.addLine(to: CGPoint(x: 0, y: layerHeight / 2))
// Mask to Path
let shapeLayer = CAShapeLayer()
shapeLayer.path = bezierPath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.orange.cgColor
shapeLayer.lineWidth = 1.0
grillVw.layer.addSublayer(shapeLayer)
//grillVw.layer.mask = shapeLayer
shapeView.addSubview(grillVw)
}
}
@ibvar-shapeView:UIView!
//约束顶部20,左侧和右侧16,高度为320
覆盖函数视图显示(u动画:Bool){
钻孔数量(格栅数量:17,格栅宽度:40,格栅高度:60)
}
func HowmanyGrillWened(grillCount:Int,grillWidth:CGFloat,grillHeight:CGFloat)
{
设xPositionDiff=Int((shapeView.frame.width/grillWidth))
变量xpositoncount:Int=0
变量ypositoncount:Int=-1
对于0中的i.。简单地使用图像资源作为背景如何?@MilanNosáľ视图大小是动态的,我们需要为通用应用程序进行设计一种方法是使用UIBezierPath绘制,另一种方法是使用图像,我认为这是一个更好的选择。@J.Doe我使用UIBezierPath绘制线条,但我的逻辑无法正常工作。您应该不要为每条线添加形状图层。为所有线创建一个贝塞尔路径。避免不必要的图层添加。为什么不创建一个子类UIView
并覆盖drawRect:
?