Search code examples
swiftanimationcore-graphicslines

How do you rotate a line with swift


I have a line created using Core Graphics and I would like to move the end point of the line so the first point stays the same and the other point moves down. This is the code I am using to create the line:

let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 2.0)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let components: [CGFloat] = [0.0, 0.0, 1.0, 1.0]
let color = CGColorCreate(colorSpace, components)
CGContextSetStrokeColorWithColor(context, color)
CGContextMoveToPoint(context, 30, 90)
CGContextAddLineToPoint(context, 300, 30)
CGContextStrokePath(context)

Solution

  • To animate a drawing rather than the container of that drawing (view or layer), you can use a CAShapeLayer and change its path; that is an animatable change. If that animation doesn't give you what you want — and it is more or less impossible to take fine control of it — you'll have to use a custom animatable property, as shown in this answer of mine.