Home > Back-end >  'CGContextAddLineToPoint' is unavailable : Swift
'CGContextAddLineToPoint' is unavailable : Swift

Time:11-18

What is replace of the CGContextAddLineToPoint this is what I'm trying to do

 for i in aerr {
     //0.5f offset lines up line with pixel boundary
    CGContextMoveToPoint(context, self.bounds.origin.x, self.font!.lineHeight * CGFloat(x)   baselineOffset)
                
    CGContextAddLineToPoint(context, CGFloat(self.bounds.size.width), self.font!.lineHeight * CGFloat(x)   baselineOffset)
 }

and I'm getting the following error. I know they clearly mention using move(to:) but how to use that?

'CGContextMoveToPoint' is unavailable: Use move(to:) instead

CodePudding user response:

you can do it like this

context.move(to: .init(x: self.bounds.origin.x, y: self.font!.lineHeight * CGFloat(x)   baselineOffset))

CodePudding user response:

In Swift, you can simply use directly from your context because it methods is part of CGContext.

So CGContextMoveToPoint(context, x, y) will be context.move(to:CGPoint(x, y))

As same as, CGContextAddLineToPoint(context, x, y) will be context.addLine(to: CGPoint(x, y))

So your code will be

guard let context = UIGraphicsGetCurrentContext() else { return }

for i in aerr {
     //0.5f offset lines up line with pixel boundary
    context.move(to: CGPoint(x:  self.bounds.origin.x, y: self.font!.lineHeight * CGFloat(x)   baselineOffset))
    
    context.addLine(to: CGPoint(x: CGFloat(self.bounds.size.width), y: self.font!.lineHeight * CGFloat(x)   baselineOffset))
 }
  • Related