首页 > 解决方案 > 翻转superview后UIButton点击区域正在移动

问题描述

我正在使用带有按钮的 cardView 的动画翻转,因为它是子视图以及其他视图。动画以及图像/文本在模拟器/设备/调试器中正确显示。

问题在于点击区域 - 翻转按钮后,点击在对角被识别。谁能建议我为什么会遇到这个问题?谢谢!

动画代码发布在下面,使用调试器检查按钮约束并且它们是正确的。

fileprivate func flippingHandler(forView view: UIView) {
    guard let cardContentView = view.subviews.first(where: { $0 is CardContentView} ) as? CardContentView,
        let initialCardView = cardContentView.snapshotView(afterScreenUpdates: false) else {
            return
    }
    swipeableView.isUserInteractionEnabled = false
    initialCardView.tag = Default.snapshotTag
    cardContentView.updateForRotation()
    cardContentView.superview?.addSubview(initialCardView)
    setupCATrasaction(forInitialView: initialCardView, finalView: cardContentView)
}

//MARK: - Helpers
fileprivate func setupCATrasaction(forInitialView initialView: UIView, finalView: CardContentView) {
    CATransaction.begin()
    CATransaction.setAnimationDuration(Default.rotationDuration)
    CATransaction.setCompletionBlock { [weak self] in
        self?.cardRotationCompletionHandler(forView: finalView)
    }
    setupInitialCardLayer(initialView.layer)
    setupFinalCardLayer(finalView.layer)
    CATransaction.commit()
}

fileprivate func cardRotationCompletionHandler(forView cardContentView: CardContentView) {
    cardContentView.superview?.subviews.filter { $0.tag == Default.snapshotTag }.forEach { $0.removeFromSuperview() }
    swipeableView.isUserInteractionEnabled = true
}

fileprivate func setupInitialCardLayer(_ startingLayer: CALayer) {
    startingLayer.isDoubleSided = false
    startingLayer.add(scalingAnimation, forKey: "scale")
    startingLayer.add(flipAnimation, forKey: "flipAnimation")
}

fileprivate func setupFinalCardLayer(_ finalLayer: CALayer) {
    finalLayer.isDoubleSided = false
    finalLayer.transform = CATransform3DMakeRotation(CGFloat(Double.pi), 0, 1, 0)
    finalLayer.add(scalingAnimation, forKey: "scale")
    finalLayer.add(flipAnimation, forKey: "flipAnimation")
}

标签: iosswift

解决方案


我能够通过丢弃所有卡片视图并在动画完成后从头开始重新加载它们来解决这个问题。

希望这个解决方案能帮助解决类似问题的人。

请参见下面的代码:

fileprivate func cardRotationCompletionHandler(forView cardContentView: CardContentView) {
    cardContentView.superview?.subviews.filter { $0.tag == Default.snapshotTag }.forEach { $0.removeFromSuperview() }
    swipeableView.isUserInteractionEnabled = true
    refreshCardViews()
}

fileprivate func refreshCardViews() {
    guard !cards.isEmpty else {
        return
    }

    //updates relevant models before refresh
    cards.forEach { $0.wasShown = false }
    swipeableView.numberOfActiveView = cards.count > Default.maxNumberOfCards ? Default.maxNumberOfCards : UInt(cards.count)

    //removes all card views from superview
    swipeableView.discardViews()

    //loads all views again
    swipeableView.loadViews()
}

推荐阅读