ios - 翻转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")
}
解决方案
我能够通过丢弃所有卡片视图并在动画完成后从头开始重新加载它们来解决这个问题。
希望这个解决方案能帮助解决类似问题的人。
请参见下面的代码:
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()
}
推荐阅读
- uistoryboard - 如何将 @IBSegueAction 与 UITabBarController 一起使用?
- r - Crontab 中的 Rscript 未在 Ubuntu Server 18.04 中运行
- c++ - 直接渲染到 WASAPI 时,两个流之一没有音频输出
- python - Selenium/Scrapy - 为某些元素返回 null
- macros - lisp 宏来构建表达式列表及其评估
- forms - 未收到电子邮件附件 magento 表单
- javascript - 使用 Javascript 通过 className 或 querySelectorAll 获取所有子元素
- masstransit - 状态机中路由单的发件箱?
- c# - FluentFTP GetListing 不返回任何结果
- spring-boot - 每 x 分钟从 Camel 队列中消费