Drawing simple lines on iPhone with CoreGraphics

后端 未结 5 2532
后悔当初
后悔当初 2021-02-20 18:09

I would like to draw a straight line between where a user touches the screen, and where the touch ends. i need multiple lines, for if the user repeats the touch-drag-release act

5条回答
  •  终归单人心
    2021-02-20 18:26

    I know this is an old question, but based on the answer I wrote the following in Swift:

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
    
        UIColor.blackColor().setStroke() // update with correct color
    
        let path = UIBezierPath()
        path.lineWidth = UIScreen.mainScreen().scale > 1 ? 0.5 : 1
    
        // change the points to what you need them to be
        let leftPoint = CGPointMake(0, CGRectGetHeight(rect))
        let rightPoint = CGPointMake(CGRectGetWidth(rect), CGRectGetHeight(rect))
    
        path.moveToPoint(leftPoint)
        path.addLineToPoint(rightPoint)
    
        path.stroke()
    }
    

提交回复
热议问题