我有一个父viewController和一个子viewController。 childViewController就像一张卡片,其功能类似于Apple的股票或地图应用程序。我可以展开或折叠它,并与其中的按钮进行交互。唯一的问题是我无法与父viewController中的任何按钮进行交互。任何人都知道有什么问题。

ios - 与具有多个子ViewController的ViewController进行交互-LMLPHP

class BaseViewController: UIViewController {

enum CardState {
    case expanded
    case collapsed
}

var cardViewController: CardViewController!
var visualEffectView: UIVisualEffectView!

lazy var deviceCardHeight: CGFloat = view.frame.height - (view.frame.height / 6)
lazy var cardHeight: CGFloat = deviceCardHeight
let cardHandleAreaHeight: CGFloat = 65

var cardVisible = false
var nextState: CardState {
    return cardVisible ? .collapsed : .expanded
}

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

@IBAction func expandCard(_ sender: Any) {
    print("Button Pressed")
}
func setupCard() {

    cardViewController = CardViewController(nibName: "CardViewController", bundle: nil)
    self.addChild(cardViewController)
    self.view.addSubview(cardViewController.view)

    //Set up frame of cardView
    cardViewController.view.frame = CGRect(x: 0, y: view.frame.height - cardHandleAreaHeight, width: view.frame.width, height: cardHeight)

    cardViewController.view.clipsToBounds = true

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleCardTap(recognizer:)))
         cardViewController.handleArea.addGestureRecognizer(tapGestureRecognizer)

}

@objc func handleCardTap(recognizer: UITapGestureRecognizer) {
    switch recognizer.state {
    case .ended:
        animationTransitionIfNeeded(state: nextState, duration: 0.9)
    default:
        break
    }
}

 func animationTransitionIfNeeded(state: CardState, duration: TimeInterval) {
    if runningAnimations.isEmpty {
        let frameAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {
            switch state {
            case .expanded:
                self.cardViewController.view.frame.origin.y = self.view.frame.height - self.cardHeight
            case .collapsed:
                self.cardViewController.view.frame.origin.y = self.view.frame.height - self.cardHandleAreaHeight
            }
        }

        frameAnimator.addCompletion { _ in
            //if true set to false, if false set to true
            self.cardVisible = !self.cardVisible
            self.runningAnimations.removeAll()
        }

        frameAnimator.startAnimation()
        runningAnimations.append(frameAnimator)

        let cornerRadiusAnimator = UIViewPropertyAnimator(duration: duration, curve: .linear) {
            switch state {
            case .expanded:
                self.cardViewController.view.layer.cornerRadius = 12
            case .collapsed:
                self.cardViewController.view.layer.cornerRadius = 0
            }
        }

        cornerRadiusAnimator.startAnimation()
        }
     }

最佳答案

“无法互动”应表示您无法按下。如果是这种情况,最可能的原因是按钮上覆盖了一些东西(它可能是透明的,因此请确保在测试之前没有透明的背景,直到您解决此问题为止)。另一个可能的原因是,您已经设置了导致该行为的按钮的某些属性,但是您可能会知道,因此几乎可以肯定是前者。

10-06 02:33