微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实时动画视图

如何解决实时动画视图

我正在尝试创建与实时相关的动画。

我的屏幕上有两个点,图像在它们之间移动。例如,我有一个图像位于 12:00 的 Strat 点,我希望它位于 12:10 的结束点。

func animate_this_image(start: Date,end: Date,imageView: UIImageView,start_point: CGPoint,end_point: CGPoint) {
    let duration = end.timeIntervalSince(start)
    let frame = CGRect(x: end_point.x,y: end_point.y,width: 60,height: 60).offsetBy(dx: -30,dy: -30)
    UIView.animate(withDuration: duration) {
        imageView.frame = frame
    }
}

我的问题是我希望它连接到实际时钟。例如,如果我在 12:02 关闭视图并在 12:09 打开它,我希望图像位于两点之间的正确位置(大约在最后)。相反,此刻图像又从起点开始。之所以发生这种情况,是因为我只是取了两个日期并将其差用作动画持续时间。

有人遇到过类似的问题或对我如何解决这个问题有建议吗?

这是连接到地图。我有两个点和点之间的折线。我想要一个图像在点之间滑动。我无法用地图找出任何简单的东西,所以我在地图顶部创建了一个视图,该视图打开了图像视图。我将点从地图转移到另一个视图以获得起点和终点。也许使用地图有更好的方法来做到这一点?

干杯, 乔纳斯

解决方法

这比我想象的要容易...这是我的解决方案

func animate_this_image(start: Date,end: Date,imageView: UIImageView,start_point: CGPoint,end_point: CGPoint) {
    // find the total duration and the time left until reaching next point
    let duration = end.timeIntervalSince(start)
    let time_left = end.timeIntervalSince(Date())
    // the percentage is used to calculate how far is left on the line
    let percentage = time_left / duration
    //calculate the correct point at this time
    let x_dist = (end_point.x - start_point.x) * CGFloat(percentage)
    let y_dist = (end_point.y - start_point.y) * CGFloat(percentage)
    imageView.frame = CGRect(x: end_point.x - x_dist,y: end_point.y - y_dist,width: 60,height: 60).offsetBy(dx: -30,dy: -30)
    // define end point
    let frame = CGRect(x: end_point.x,y: end_point.y,dy: -30)
    // Then animate it
    UIView.animate(withDuration: time_left) {
        imageView.frame = frame
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。