gpt4 book ai didi

ios - viewController 在我关闭时显示

转载 作者:行者123 更新时间:2023-11-28 07:36:38 24 4
gpt4 key购买 nike

我有一个 viewcontroller,它是 tableViewController 的 segue,我想用过渡来呈现它,并作为顶部的 sideMenu 我找到了一些代码并且它工作正常但是当我关闭 ViewController 时我无法理解它自己呈现的部分

//
// ViewController.swift
// ProTansition
//
// Created by Teodik Abrami on 11/1/18.
// Copyright © 2018 Teodik Abrami. All rights reserved.
//

import UIKit

class ViewController: UIViewController, MenuTransitionManagerDelegate {

func dismiss() {
dismiss(animated: true, completion: nil)
print("dismiss run")
}
var menuTransition = MenuTransitionManager()

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

override func viewDidAppear(_ animated: Bool) {
print("ViewController Appear")
}

override func viewDidDisappear(_ animated: Bool) {
print("viewcontroller disapear")
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinaion = segue.destination
destinaion.transitioningDelegate = menuTransition
menuTransition.delegate = self
}
}

tableView 是一个普通的 tableview,有 5 行,里面没有特殊代码

和过渡

//
// MenuTransitionManager.swift
// ProTansition
//
// Created by Teodik Abrami on 11/1/18.
// Copyright © 2018 Teodik Abrami. All rights reserved.
//

import Foundation
import UIKit
@objc protocol MenuTransitionManagerDelegate {

func dismiss()
}

class MenuTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
let duration = 2.0
var isPresenting = false
var delegate: MenuTransitionManagerDelegate?
var snapShot: UIView? {
didSet {
if let delegate = delegate {
let tap = UITapGestureRecognizer(target: delegate, action: #selector(delegate.dismiss))
snapShot?.addGestureRecognizer(tap)
}
}
}

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

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
return
}

guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
return
}
let container = transitionContext.containerView
let moveDown = CGAffineTransform.init(translationX: 0, y: container.frame.height - 150)
if isPresenting {
container.addSubview(toView)
snapShot = fromView.snapshotView(afterScreenUpdates: true)
container.addSubview(snapShot!)
}

UIView.animateKeyframes(withDuration: duration, delay: 0, options: [], animations: {
if self.isPresenting {
self.snapShot?.transform = moveDown
} else {
self.snapShot?.transform = CGAffineTransform.identity
}
}) { (finished) in
transitionContext.completeTransition(true)
if !self.isPresenting {
self.snapShot?.removeFromSuperview()
}
}
}

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

isPresenting = true
return self
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

isPresenting = false
return self
}

}

我将 tapsture 添加到快照,当它的 tapped 协议(protocol)工作时,在 viewcontroller 中关闭时工作,viewController 出现我不明白为什么甚至为什么代码在未显示的 Controller 上运行

this is the view controller that i cant figured out why it present when i dismiss it

this happend when menuButton pressed

最佳答案

设置 isPresenting = trueisPresenting = false 的整个策略注定要失败,因为这两种代码都将在两种情况下运行。您必须通过使用两个不同的 animationController 对象(而不是两次都返回 self )或查看哪个 View Controller 是 from< 来区分呈现和解除 View Controller ,它是 to View Controller 。

关于ios - viewController 在我关闭时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110878/

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