簡體   English   中英

當按下其他視圖 controller 中的按鈕時,視圖 controller 中的點擊手勢導致視圖 controller 的打開延遲

[英]Tap gesture in a view controller causing delay in opening of the view controller when a button in other view controller is pressed

我在一個嵌入選項卡欄的 ViewController 中實現了 UIStackView 上的點擊手勢。 當我單擊選項卡欄上的按鈕打開它時,它會導致幾秒鍾的延遲,然后打開視圖 Controller。在 inte.net 上搜索了一段時間后,我發現點擊手勢可能會導致此問題,顯然當我刪除此點擊手勢時,它工作順利。 他們建議的解決方案只是刪除點擊手勢,因為在大多數情況下他們不想要它並且它不小心在那里但是我確實想要點擊手勢所以我無法刪除它。 這就是我在 viewDidLoad() 中添加簡單點擊手勢的方式:

        let tapAddImage = UITapGestureRecognizer(target: self, action: #selector(self.addImage(_:)))
        tapAddImage.cancelsTouchesInView = false
        svAddImage.addGestureRecognizer(tapAddImage)

這個 function 打開圖像選擇器。

此外,我剛剛注意到在其他 ViewControllers 中也引起了同樣的問題,我已經實現了 imagePicker,它是通過使用 Tap Gesture 點擊某些東西打開的。 這是我的圖像選擇器委托函數:

 @objc func addImage(_ sender: UITapGestureRecognizer) {
        openCameraOrGallery()
    }

   func openCameraOrGallery() {
        let alert = UIAlertController(title: BaseUrl.shared.projectName, message: "Select Option", preferredStyle: .actionSheet)
               
        alert.addAction(UIAlertAction(title: "Camera", style: .default , handler:{ (UIAlertAction)in
            self.present(self.imagePicker!, animated: true, completion: {
                self.imagePicker?.sourceType = .camera
                self.imagePicker?.delegate = self
            })
        }))
               
        alert.addAction(UIAlertAction(title: "Gallery", style: .default , handler:{ (UIAlertAction)in
            self.present(self.imagePicker!, animated: true, completion: {
                self.imagePicker?.sourceType = .photoLibrary
                self.imagePicker?.delegate = self
            })
        }))
               
        alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
            print("User click Dismiss button")
        }))
        
        self.present(alert, animated: true, completion: {
            print("completion block")
        })
    }
    
    //MARK: UIImagePickerControllerDelegate
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        guard let tempImage:UIImage = info [.originalImage] as? UIImage else {return}
        
        self.ivSelectedPostImage.image = tempImage
        
        let imageData:NSData = tempImage.jpegData(compressionQuality: 0.2)! as NSData
        postBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
        
        self.svAddPostContentButtons.isHidden = true
        self.viewSelectedPostImage.isHidden = false
        
        self.dismiss(animated: true, completion: nil)
    }
    
    func cancelButtonDidPress(_ imagePicker: UIImagePickerController) {
        imagePicker.dismiss(animated: true, completion: nil)
    }

我剛剛從我的其他項目中復制粘貼了這段代碼。 使用 ImagePicker Delegates 或點擊手勢的任何其他項目都沒有問題。 知道什么可能僅在我使用點擊手勢打開相機或畫廊的這些頁面中導致延遲嗎? 這只發生在那些 ViewControllers 上,我點擊一個按鈕打開這些具有點擊手勢的 ViewControllers。

請嘗試在 ViewDidAppear 中初始化點擊手勢。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let tapAddImage = UITapGestureRecognizer(target: self, action: #selector(self.addImage(_:)))
    tapAddImage.cancelsTouchesInView = false
    svAddImage.addGestureRecognizer(tapAddImage)

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM