我正在尝试进行自定义转换,我想要一个视图从底部进入焦点。但正如在视频中看到的那样,视图的快照并不完全等于viewcontroller中呈现的内容,因此它最终会产生一个混蛋。
视频链接: https://gfycat.com/RewardingSplendidBoar
你可以看到给予按钮给出一点混蛋。这在iOS 10中不会发生。
我已经添加了 toVC.view.setNeedsLayout() toVC.view.layoutIfNeeded() 但仍无济于事。
这是我的代码:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
var selected: IndexPath?
if collectionView.indexPathsForSelectedItems?.count == 0 {
selected = IndexPath(row: 0, section: 0)
}
else {
selected = collectionView.indexPathsForSelectedItems?[0]
}
print("Selected cell: \(String(describing: selected?.row))")
let cell = collectionView.cellForItem(at: selected!)
let container: UIView? = transitionContext.containerView
var fromVC : UIViewController;
var toVC : UIViewController;
if isPresenting {
fromVC = (transitionContext.viewController(forKey: .from) as! ViewController)
toVC = (transitionContext.viewController(forKey: .to) as! EventDetailVC)
toVC.view.layoutIfNeeded()
}else{
fromVC = (transitionContext.viewController(forKey: .from) as! EventDetailVC)
toVC = (transitionContext.viewController(forKey: .to) as! ViewController)
}
let fromView: UIView? = fromVC.view
let toView: UIView? = toVC.view
let beginFrame: CGRect? = container?.convert((cell?.contentView.bounds)!, from: cell?.contentView)
var endFrame: CGRect = transitionContext.initialFrame(for: fromVC)
endFrame = toView?.frame ?? CGRect.zero
var move: UIView? = nil
var iconBack: UIView? = nil
var btnGive: UIView? = nil
var transitionDuration: CGFloat
if isPresenting {
transitionDuration = CGFloat(self.duration)
toView?.frame = endFrame
move = toView?.snapshotView(afterScreenUpdates: true)
(toVC as! EventDetailVC).ivBack.alpha = 1
iconBack = (toVC as! EventDetailVC).ivBack?.snapshotView(afterScreenUpdates: true)
(toVC as! EventDetailVC).btnGive.alpha = 1
btnGive = (toVC as! EventDetailVC).btnGive?.snapshotView(afterScreenUpdates: true)
move?.frame = beginFrame!
move?.alpha = 0.0
container?.addSubview(move!)
container?.addSubview(iconBack!)
container?.addSubview(btnGive!)
}
else {
transitionDuration = CGFloat(self.duration)
move = fromView?.snapshotView(afterScreenUpdates: true)
move?.frame = (fromView?.frame)!
fromView?.removeFromSuperview()
container?.addSubview(move!)
}
if isPresenting {
toVC.view.setNeedsLayout()
toVC.view.layoutIfNeeded()
var frame = (toVC as! EventDetailVC).ivBack.frame;
frame.origin.x = (toVC as! EventDetailVC).view.frame.size.width
iconBack?.frame = frame
var frame1 = (toVC as! EventDetailVC).btnGive.frame
frame1.origin.y = (toVC as! EventDetailVC).view.frame.size.height
btnGive?.frame = frame1
UIView.animate(withDuration: TimeInterval(transitionDuration) , animations: {() -> Void in
move?.frame = (toVC as! EventDetailVC).imageView.frame
move?.alpha = 1.0
cell?.alpha = 0.0
iconBack?.frame = (toVC as! EventDetailVC).ivBack.frame
btnGive?.frame = (toVC as! EventDetailVC).btnGive.frame
}, completion: {(_ finished: Bool) -> Void in
iconBack?.removeFromSuperview()
btnGive?.removeFromSuperview()
move?.removeFromSuperview()
toView?.frame = endFrame
container?.addSubview(toView!)
transitionContext.completeTransition(true)
})
}
else {
cell?.isHidden = false
cell?.alpha = 0.0
UIView.animate(withDuration: TimeInterval(transitionDuration), animations: {
move?.frame = beginFrame!
move?.alpha = 0.0
cell?.alpha = 1.0
}, completion: { (_ finished: Bool) in
cell?.isHidden = false
transitionContext.completeTransition(true)
})
}
}