【问题标题】:How to animate display of same View Controller but with updated content如何动画显示相同的视图控制器但具有更新的内容
【发布时间】:2018-11-22 01:49:00
【问题描述】:

我想对屏幕内容的更新进行动画处理,就像过渡到不同的视图控制器一样。例如,想象一个包含今天事件的日历页面场景。向左滑动会将内容更新为明天的事件,但视图控制器和场景是相同的。不过,我想为从右侧滑入的新内容制作动画。

我尝试将整个视图向左移动。这很有效,但它在下面显示了一个黑屏,虽然很快被正确的内容替换了,但还是很刺耳。

我记得在 HyperCard 时代,您可以锁定卡片的显示,更新内容,然后通过转换(例如溶解或擦除)解锁显示。我知道这可能不是 iOS 做事的方式,但我提到它只是为了帮助描述我正在尝试做的事情。

添加进行手势识别和移动的代码...

@objc func swipeDetected(gesture: UIGestureRecognizer) {

    let calendar = Calendar.current

    if let swipeGesture = gesture as? UISwipeGestureRecognizer {

        switch swipeGesture.direction {
        case .left:
            // move current page to the left
            animateViewMoving(right: false, timeInterval: 0.3)
            // update content
            currentDate = calendar.date(byAdding: .day, value: 1, to: currentDate)!
            populateSchedItems()
            self.tableView.reloadData()
            // return page with no duration
            animateViewMoving(right: true, timeInterval: 0.0)

        case .right:
            // move page to left with no duration
            animateViewMoving(right: false, timeInterval: 0.0)
            // update content
            currentDate = calendar.date(byAdding: .day, value: -1, to: currentDate)!
            populateSchedItems()
            self.tableView.reloadData()
            // slide new content in from the left
            animateViewMoving(right: true, timeInterval: 0.3)

        default:
            break
        }
    }
}

func animateViewMoving (toTheRight: Bool, timeInterval: Double) {
    let frameWidth = self.view.frame.width
    let movementDuration:TimeInterval = timeInterval
    let movement:CGFloat = ( toTheRight ? frameWidth : -frameWidth)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = self.view.frame.offsetBy(dx: movement, dy: 0)
    UIView.commitAnimations()
}

【问题讨论】:

  • “我尝试过将整个视图向左移动的动画效果” 请给我们看看这段代码好吗?
  • 移动是对“向左滑动”手势的响应。这是手势处理代码和执行移动的函数
  • “我记得在 HyperCard 时代,您可以锁定卡片的显示,更新内容,然后通过转换解锁显示,例如溶解或擦除” iOS:apeth.com/iOSBook/ch17.html#_transitions 但是,任何内置转换都不太可能完全符合您的要求。

标签: swift4 ios11 xcode9 uiviewanimation


【解决方案1】:

获取当前内容的快照视图并将其放在当前视图的后面。 (这将使我们看到动画背后的黑色窗口以外的东西。)现在完全按照您已经在做的事情:将真实视图移到一边并为其设置动画。现在悄悄地删除快照视图。

【讨论】:

  • 谢谢!我会试试看。
  • 这就像一个冠军,并提出了你的想法的一些变化。例如,通过抓取屏幕图像并以编程方式在其他场景子视图前面创建 UIImage,然后我可以更新这些子视图内容并滑动 UImage 以显示它们。这导致了一个小型单 ViewController 内容更新库过渡。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 1970-01-01
相关资源
最近更新 更多