【问题标题】:Animating a view in a real time实时动画视图
【发布时间】:2021-06-20 18:56:03
【问题描述】:

我正在尝试创建连接到实时的动画。

我的屏幕上有两个点,图像在它们之间移动。例如,我有一个图像在 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 打开它,我希望图像位于两点之间的正确位置(大约在最后)。相反,此时图像再次从起点开始。发生这种情况是因为我只取两个日期并将差值用作动画持续时间。

任何有类似问题的人或关于如何解决此问题的建议?

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

干杯, 乔纳斯

【问题讨论】:

    标签: xcode animation mapkit swift5 uiviewanimation


    【解决方案1】:

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

    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, width: 60, height: 60).offsetBy(dx: -30, dy: -30)
        // Then animate it
        UIView.animate(withDuration: time_left) {
            imageView.frame = frame
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-08
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 2016-03-26
      相关资源
      最近更新 更多