gpt4 book ai didi

ios - 自定义 UIViewController 动画问题

转载 作者:搜寻专家 更新时间:2023-10-31 22:38:09 24 4
gpt4 key购买 nike

我使用 UIViewControllerAnimatedTransitioningUIViewController 创建了一个简单的自定义过渡动画。一切正常,但是当目标 View 出现时,内容从右下角开始出现,当 View 消失时,内容从左上角消失。我附上了 gif 示例。这是我的代码:

CustomViewAnimator.swift

class CustomViewAnimator: NSObject, UIViewControllerAnimatedTransitioning {

private let interval = 0.5
var forward = false
var originFrame: CGRect = .zero

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return interval
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

let containerView = transitionContext.containerView

guard let toView = transitionContext.view(forKey: .to) else {return}
guard let fromView = transitionContext.view(forKey: .from) else {return}
let destinationView = forward ? toView : fromView
destinationView.alpha = forward ? 0 : 1

forward ? containerView.addSubview(toView) : containerView.insertSubview(toView, belowSubview: fromView)
forward ? (destinationView.frame = originFrame) : (destinationView.frame = fromView.frame)

UIView.animate(withDuration: interval, animations: {

self.forward ? (destinationView.frame = fromView.frame) : (destinationView.frame = self.originFrame)
destinationView.alpha = self.forward ? 1 : 0

}) { (finished) in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}

}

}

MainViewController.self

class MainViewController: UIViewController {

private var button: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
setup()
}

@objc private func openView() {
let viewController = TargetViewController()
viewController.transitioningDelegate = self
navigationController?.pushViewController(viewController, animated: true)

}
}

extension MainViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

let animator = CustomViewAnimator()
animator.originFrame = button.frame
animator.forward = (operation == .push)
return animator
}
}

extension MainViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let animator = CustomViewAnimator()
animator.forward = true
return animator
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let animator = CustomViewAnimator()
animator.forward = false
return animator
}
}

TargetViewController.swift

class TargetViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
setup()
}

}

extension TargetViewController {

fileprivate func setup() {
view.backgroundColor = .orange
navigationItem.title = "Target view"

let label: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Hello from target view :)"
label.textColor = .white
label.font = UIFont.boldSystemFont(ofSize: 18)
return label
}()
view.addSubview(label)

label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}

}

谁能告诉我如何解决这个问题,使 TargetViewController 中的标签(或任何其他内容)保持在原位?

enter image description here

最佳答案

基本上你的问题是因为混合了自动布局和框架。您在 ViewController 的 View 中使用约束定位 subview (按钮和标签),但对于动画,您使用帧更新。因此,据我正确理解布局的工作原理,您的标签约束在转换期间未激活,或者至少布局引擎未使用它来计算标签的位置。

因此,最好的建议是避免过渡中的帧动画,并尝试为 .from.to View 创建适当的约束,并对约束常量进行动画处理。这将需要更多代码,但您可以确定您不会混合使用两种不同的布局策略。

更简单的解决方案是告诉 containerView 计算约束并在动画期间适本地定位元素,只需将 containerView.layoutIfNeeded() 添加到动画 block 即可。所以它会是:

UIView.animate(withDuration: interval, animations: {
self.forward ? (destinationView.frame = fromView.frame) : (destinationView.frame = self.originFrame)
destinationView.alpha = self.forward ? 1 : 0
containerView.layoutIfNeeded()
}) { (finished) in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}

但这只能使事情正常进行,并不能解决同时使用两种布局策略的主要问题。

关于ios - 自定义 UIViewController 动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53142437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com