UIButton点击区域在翻转超级视图后正在移动



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

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

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

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")
}

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

希望此解决方案可以帮助某人解决类似问题。

请参阅下面的代码:

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()
}

最新更新