Adriana Pineda Adriana Pineda - 4 months ago 21
Swift Question

watchOS2 animateWithDuration starts slow and speeds up

I'm trying to animate the width of a group in my watchOS2 application by calling

animateWithDuration
of
WKInterfaceController
class. The idea is to show the user an horizontal line which decreases its width from right to left over a period of time (something like a timer):

self.timer.setWidth(100)
self.animateWithDuration(NSTimeInterval(duration)) {
self.timer.setWidth(0)
}


However I'm seeing that as soon as the animation starts the speed is very slow and then it increases. When the animation is about to stop (when the timer width is close to 0) the animation slows down again.
I want the speed to be the same over the duration of the animation.

Has anyone had this issue before? Any help is appreciated! Thanks

Answer

I made a simple example of the timer I wanted to animate with Core Graphics in the watchOS2 app. You can find the project here

UPDATE: Here's the code that I made:

func configureTimerWithCounter(counter: Double) {

    let size = CGSizeMake(self.contentFrame.width, 6)
    UIGraphicsBeginImageContext(size)
    let context = UIGraphicsGetCurrentContext()
    UIGraphicsPushContext(context!)

    // Draw line
    let path = UIBezierPath()
    path.lineWidth = 100

    path.moveToPoint(CGPoint(x: 0, y: 0))

    let counterPosition = (self.contentFrame.width/30)*CGFloat(counter)
    path.addLineToPoint(CGPoint(x: counterPosition, y: 0))

    UIColor.greenColor().setStroke()
    UIColor.whiteColor().setFill()
    path.stroke()
    path.fill()

    // Convert to UIImage
    let cgimage = CGBitmapContextCreateImage(context);
    let uiimage = UIImage(CGImage: cgimage!)

    // End the graphics context
    UIGraphicsPopContext()
    UIGraphicsEndImageContext()

    self.timerImage.setImage(uiimage)

}