点网格外的线条

时间:2017-07-13 11:23:11

标签: ios swift grid lines

我在网格中的点之间设置线条时遇到“错误”。如果我在网格外单击,我可以让线条“无处”。我必须在网格中的2个点之间设置线条(水平和垂直)。我试图找出问题所在,但无法确定问题究竟在哪里,要解决它

enter image description here

  func setup() {
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(showMoreActions(touch:)))
    tapGestureRecognizer.numberOfTapsRequired = 1
    addGestureRecognizer(tapGestureRecognizer)
}


//draw line
func addLine(start: CGPoint,end:CGPoint) {

    let line = CAShapeLayer()
    let linePath = UIBezierPath()

    linePath.move(to: start)
    linePath.addLine(to: end)
    line.path = linePath.cgPath
    line.strokeColor = UIColor.black.cgColor
    line.lineWidth = 2
    line.lineJoin = kCALineJoinRound
    layer.addSublayer(line)

}



//  draw line between points
func showMoreActions(touch: UITapGestureRecognizer) {

    let touchPoint = touch.location(in: self)

    let (start, end) = findEndPoints(touchPt: touchPoint)
    addLine(start: start, end: end)

}

func findEndPoints(touchPt: CGPoint) -> (pt1: CGPoint, pt2: CGPoint) {

    //calculate where the touch is done

    let viewWidth =  self.bounds.width
    let viewHeight = self.bounds.height

    let gridRowWidth: CGFloat = viewWidth - diameter/4
    let gridColumnHeight: CGFloat = viewHeight - diameter/4

    let cellWidth: CGFloat = gridRowWidth / CGFloat(pointsOnRow)
    let cellHeight: CGFloat = gridColumnHeight / CGFloat(pointsOnColumn)

    // convert touch point to grid coordinates
    let gridX = ( CGFloat(touchPt.x) /  CGFloat(cellWidth) ) - 0.5
    let gridY = ( CGFloat(touchPt.y) / CGFloat(cellHeight) ) - 0.5

    // snap to nearest point in the grid
    let snapX = round(gridX)
    let snapY = round(gridY)

    // find distance from touch to snap point

    let distX = abs(gridX - snapX)
    let distY = abs(gridY - snapY)


    // start second point on top of first
    var secondX = snapX
    var secondY = snapY


    if distX < distY {
        // this is a vertical line
        if secondY > gridY {
            secondY -= 1
        } else {
            secondY += 1
        }
    } else {
        //this is a horizontal line
        if secondX > gridX {
            secondX -= 1
        } else {
            secondX += 1
        }
    }

    let halfdot = CGFloat(diameter) / 2

    // convert line points to view coordinates
    let pt1 = CGPoint(x: (snapX + 0.5) * CGFloat(cellWidth) + halfdot, y: (snapY + 0.5) * CGFloat(cellHeight) + halfdot)

    let pt2 = CGPoint(x: (secondX + 0.5) * CGFloat(cellWidth) + halfdot, y: (secondY + 0.5) * CGFloat(cellHeight) + halfdot)

    return (pt1, pt2)
}

1 个答案:

答案 0 :(得分:0)

网格功能重新开始:

let squareSide = self.bounds.width
    let distanceBetweenPointsOnRow = ( squareSide - 2 * pointStartPosition ) / (pointsOnRow - 1)
    let distanceBetweenPointsOnColumn = ( squareSide - 2 * pointStartPosition ) / (pointsOnColumn - 1)

    for n in 0..<self.pointsOnRow {
        for m in 0..<self.pointsOnColumn {

            let circleX: CGFloat = pointStartPosition + (CGFloat(n) * distanceBetweenPointsOnRow - diameter/2)
            let circleY: CGFloat = pointStartPosition + (CGFloat(m) * distanceBetweenPointsOnColumn - diameter/2)

现在计算放在哪一行的函数是:

  let squareSide = self.bounds.width

    let distanceBetweenPoints = ( squareSide - 2 * pointStartPosition ) / (CGFloat(pointsOnColumn) - 1)

    // convert touch point to grid coordinates
    let gridX = (touchPt.x - pointStartPosition)/distanceBetweenPoints
    let gridY = (touchPt.y - pointStartPosition)/distanceBetweenPoints

带支票:

    // check if the user is clicked outside the point grid
    if secondX < 0 || secondY < 0 || secondY > (CGFloat(pointsOnColumn) - 1) || secondX > (CGFloat(pointsOnRow) - 1) || snapX < 0 || snapY < 0 || snapY > (CGFloat(pointsOnColumn) - 1) || snapX > (CGFloat(pointsOnRow) - 1) {
        return nil
    }