问题
I implemented a project I used PDFKIT to do some how adding a floating Textbox on a pdf and it can navigate by users touch on screen using GestureRecognizer
to move and put where users decide to. it has worked perfectly before iOS 13, but after it, it does not give you the chance to move the text box and I tried many ways to solve it but all failed.
It seems that the problem somehow relates to GestureRecognizer
but I am not sure, and also I really do not know how to fix it. I was working on this project on github https://github.com/kishikawakatsumi/BookReader and tried to expand it some how.
and here is my codes project, I am inserting the whole functions, I know it seems a little complicated. Really sorry about this. But if you think it might be from gesture recogniser, just look at the codes belong to these functions.
func getPDFFrame(todo: String)
{
let pdfSubVws = pdfView.subviews
print("subVws ",pdfSubVws)
for subVws in pdfSubVws
{
if (String(describing: subVws).range(of:"UIPageViewControllerContentView") != nil)
{
let pageVc = subVws.subviews
for subVws in pageVc
{
if (String(describing: subVws).range(of:"UIQueuingScrollView") != nil)
{
let QueueVc = subVws.subviews
// print("QueueVcQueueVc ", QueueVc)
for subVws in QueueVc
{
if (String(describing: subVws).range(of:"UIView") != nil)
{
let viewVc = subVws.subviews
print("\n\nviewVcVcQueueVc ", viewVc) //viewVcVcQueueVc []
for subVws in viewVc
{
if (String(describing: subVws).range(of:"UIView") != nil)
{
let SubviewVc = subVws.subviews
// print("\n\nSubviewVcSubviewVc ", SubviewVc)
for subVws in SubviewVc
{
if (String(describing: subVws).range(of:"UIScrollView") != nil)
{
let finalVw = subVws.subviews
// print("\n\nfinalVw ", finalVw)
//
for subVws in finalVw
{
if (String(describing: subVws).range(of:"UIView") != nil)
{
// print("subVwssubVws ", subVws)
let pagView = subVws.subviews
// pdfLandingView =
for subVws in pagView
{
// print("\n\n EndVwssubVws ", subVws)
if (String(describing: subVws).range(of:"PDFPageView") != nil)
{
let pageLandVw = subVws.subviews
print("\n\n WasVwssubVws ", pageLandVw)
// hideBars()
// Myedit()
for subVws in pageLandVw
{
if (String(describing: subVws).range(of:"UITextView") != nil)
{
print("dddddddd")
let txtVw = subVws as! UITextView
if todo == "firstResponder"
{
let rect = pdfView.currentPage?.bounds(for: .mediaBox)
usingHeightText = txtVw.frame.size.height
usingWidthText = txtVw.frame.size.width
let xPoint = txtVw.frame.origin.x
let yPoint = (rect?.height)! - (txtVw.frame.origin.y) - txtVw.frame.size.height
lastTextAnnoPoint = CGPoint(x: xPoint, y: yPoint)
findHavingTextAnnotation(annoPoint: CGPoint(x: xPoint, y: yPoint))
// txtVw.contentInset = UIEdgeInsetsMake(0, 10, 0, 0)
print("C_Sz ", txtVw.contentSize)
print("C_Off ", txtVw.contentOffset)
let widthMoveView = UIView(frame: CGRect(x: txtVw.frame.size.width - 20, y: 0, width: 20, height: txtVw.frame.size.height))
widthMoveView.backgroundColor = UIColor.brown.withAlphaComponent(0.0)
widthMoveView.tag = 333
let wdImgVw = UIImageView()
wdImgVw.frame.origin.x = 0
wdImgVw.frame.origin.y = 8
wdImgVw.frame.size.width = 20 // widthMoveView.frame.width
wdImgVw.frame.size.height = 20 // widthMoveView.frame.height
wdImgVw.contentMode = .scaleAspectFit
wdImgVw.image = UIImage(named: "widthLeftRight")
wdImgVw.layer.cornerRadius = 10
// wdImgVw.backgroundColor = UIColor.orange
widthMoveView.addSubview(wdImgVw)
txtVw.addSubview(widthMoveView)
//
let tapVw = UIPanGestureRecognizer(target: self, action: #selector(self.handleWidthTapVw(_:)))
widthMoveView.addGestureRecognizer(tapVw)
}
else
//....and here I am going to omit the other parts of my codes
and here I am going to omit the other parts of my codes because seems that the problem occurs in this line of code
for subVws in finalVw
and here other parts of my codes not sure you may need it or not
let linkAnn = PDFAnnotation.init(bounds: CGRect(x: 150, y: 450, width: 180, height: usingHeightText), forType: PDFAnnotationSubtype.widget, withProperties: nil)
linkAnn.widgetFieldType = .text
linkAnn.isMultiline = true
linkAnn.contents = "fileNumber\(pdfClickedRow)PageNumber\(getPagenumber)addtext\(AddTextCount)" //"\(AddTextCount)"
linkAnn.widgetStringValue = NSLocalizedString("placeholder", comment: "")
linkAnn.font = UIFont(name: usingFontName, size: CGFloat(usingFontSize))
linkAnn.fontColor = usingFontColor
linkAnn.backgroundColor = usingBGFontColor.withAlphaComponent(usingBGFontAlpha)
linkAnn.setValue("addtext\(AddTextCount)", forAnnotationKey: PDFAnnotationKey.name)
// print("linkAnn.cont ",linkAnn.contents)
let index = pdfDocument?.index(for: pdfView.currentPage!)
pdfView.document?.page(at: index!)?.addAnnotation(linkAnn)
var createdColorDict = [String : Any]()
createdColorDict["BGColor"] = usingBGFontColor
createdColorDict["BGOpacity"] = usingBGFontAlpha
textAnnoStoredDict["fileNumber\(pdfClickedRow)PageNumber\(getPagenumber)addtext\(AddTextCount)"] = createdColorDict
// pdfView.currentPage?.addAnnotation(linkAnn)
bottomColorView.isHidden = true
bottomColorView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
sideWidthHeightVw.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0)
sideWidthHeightVw.isHidden = true
barHideOnTapGestureRecognizer.isEnabled = false
hideBars()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow),
name: NSNotification.Name.UIKeyboardWillShow, object: nil)
//keyboardWillShow
回答1:
Its hard for me to understand what could be the problem by looking at your code. Here's my high level suggestion: 1. Make a backup of your code first to make sure you can come back to your stable state. 2. Start cutting the code in half - eliminating half of the logic path each time. For example: Instead of using a PDF view, use a regular view with a text box (to find out if the problem is with the PDF view). Instead of using a UITextField, use just a UIView to see if it's a problem with UITextField.
Basically remove all the complexity gradually until you are left with a simple piece of code that allows you to move the text field.
Also: avoid having such long methods. I try to keep methods down to 30 lines max, and 2 levels of nesting max. Otherwise you will have more logic paths than you can maintain or understand...
You are also relying on strings for object types. This is fragile. Maybe there's another way to find the PDF View instead of crawling the whole view hierarchy for it, and checking strings? (They are not compiler checked, so any change in the future or change between OS / Third party versions will break your code).
If your text field doesn't move at all, try to remove all that math you're doing with the frame, and just get it to move using gesture recognizer to any coordinate (for example: 200, 200), just to see that something is holding the text field in place (like a constraint). Do you have constraints that can be holding it? Use the "View Inspector" to check and verify how the view is embedded and what's above / below it. Is there anything that could be intercepting touches to your gesture recognizer? Is it receiving touches and firing it's event? You need to describe more exactly what the problem is... Please refer to my answer here for things to check on the Gesture recognizer itself. UIPanGestureRecognizer is not working in iOS 13
Your view hierarchy is so complex with this PDF view that it's hard to say what could be happening with other Touch events inside it . (unless you understand exactly how the third party PDF View works). I would try to isolate the problem, whether it's related to the PDF view or not. Get it working in just a regular View first. Then work on the PDF view by using hitTest, and put a print statement there, to see who is competing for the HIT test.
来源:https://stackoverflow.com/questions/58609831/navigating-text-box-on-a-pdf-is-not-working-any-more-after-ios-13-seems-gestur