我正在做我的第一个自定义 segue 动画。我想要的是有一个列表视图,当我在一行上选择时,它不是从侧面滑入,而是从选定的行中展开到位。就像是:
因此用户选择了绿色行,这会导致新控制器以绿色行大小开始并展开以填满屏幕。到目前为止,这部分工作得很好。
我遇到的问题是 unwind segue 试图做相反的事情,将全屏缩小到原始行,然后关闭。发生的情况是它缩小得很好,但暴露的区域仍然是黑色,然后在动画完成时突然闪烁到位。想要的是这样的:
但正在发生的事情更像是这样:
我用于展开的代码perform
如下所示:
class FromScheduleFocusSegue: UIStoryboardSegue {
override func perform() {
let fromView = self.source.view!
let toView = self.destination.view!
let window = UIApplication.shared.keyWindow!
let schedulesList = self.destination as! SchedulesListController
let cell = schedulesList.tableView.cellForRow(at: schedulesList.tableView.indexPathForSelectedRow!)!
var stripe = cell.bounds
stripe.size.height = 60
let targetFrame = cell.convert(stripe, to: window).insetBy(dx: 0, dy: 0)
schedulesList.tableView.selectRow(at: nil, animated: false, scrollPosition: .none)
UIView.animateWithDuration(4000.milliseconds, delay: 0.seconds, options: .curveEaseInOut, animations: {
fromView.frame = targetFrame
}) { (finished) in
self.source.dismiss(animated: false, completion: nil)
}
}
}
所以基本上,我如何让我正在展开的视图在动画发生时显示出来?
为了完整起见,这里是前向 segue 代码:
class ToScheduleFocusSegue: UIStoryboardSegue {
override func perform() {
let fromView = self.source.view!
let toView = self.destination.view!
let window = UIApplication.shared.keyWindow!
let box = UIScreen.main.bounds
let schedulesList = self.source as! SchedulesListController
let cell = schedulesList.tableView.cellForRow(at: schedulesList.tableView.indexPathForSelectedRow!)!
toView.frame = cell.convert(cell.bounds, to: window).insetBy(dx: 0, dy: 0)
window.insertSubview(toView, aboveSubview: fromView)
UIView.animateWithDuration(4000.milliseconds, delay: 0.seconds, options: .curveEaseInOut, animations: {
toView.frame = box
}) { (finished) in
self.source.present(self.destination, animated: false, completion: nil)
}
}
}
(这是在 XCode8 中,针对使用 Swift3 的 iOS10)