watchOS2 animateWithDuration启动缓慢并加速

时间:2016-07-31 03:04:45

标签: swift animation watch-os-2

我正在尝试通过调用animateWithDuration类的WKInterfaceController来设置watchOS2应用程序中组的宽度。这个想法是向用户显示一条水平线,在一段时间内(如计时器)从右到左减小宽度:

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

但是我看到动画开始后速度非常慢然后会增加。当动画即将停止时(当计时器宽度接近0时),动画再次减速。 我希望在动画的持续时间内速度相同。

以前有人有这个问题吗?任何帮助表示赞赏!感谢

2 个答案:

答案 0 :(得分:1)

WatchOS 2没有提供指定计时功能的方法,因此动画仅限于使用EaseInEaseOut曲线(开始速度慢,加速,然后在结束时减速)。

您可以尝试using Core Graphics to render the line,或使用一系列WKInterfaceImage帧来平滑地为该线设置动画。

答案 1 :(得分:-1)

我在watchOS2应用程序中制作了一个我希望使用Core Graphics制作动画的计时器的简单示例。 您可以找到项目here

<强>更新: 这是我制作的代码:

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)

}
相关问题