Search code examples
iosiphoneswift2xcode7.1beta

How to animate a CGContext created in a UIView using swift?


I have created a image as CGContext in a class which is a subclass of UIView class. I need to know how to animate this image. I need to animate this image such that it moves along a circle/arc.

the image is created using the following code:

let ctx = UIGraphicsGetCurrentContext()  
let rad = CGRectGetWidth(valueRect)/0.08  
let endAngle = CGFloat(2*M_PI)
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 0), 10, UIColor(red: 0, green: 0, blue: 0, alpha: 0.35).CGColor)
CGContextAddArc(ctx, CGRectGetMidX(valueRect), CGRectGetMidY(valueRect), rad, 0, endAngle, 1)
CGContextSetFillColorWithColor(ctx,UIColor.whiteColor().CGColor)
CGContextFillPath(ctx)
CGContextSetLineWidth(ctx, 0.1)
CGContextStrokePath(ctx);

I need to animate the circle image in ctx along an arc of certain radius with a certain start and end angle. Can anyone suggest a way to do it?


Solution

  • The easiest way is to animate the view that displays the image. You can easily animate a view along a path using keyframe animation.