我在一個嵌入標簽欄的 ViewController 中實作了 UIStackView 上的點擊手勢。當我單擊選項卡欄上的按鈕打開它時,它會導致幾秒鐘的延遲,然后打開視圖控制器。在互聯網上搜索了一段時間后,我發現點擊手勢可能會導致這種情況,顯然當我洗掉這個點擊手勢時,它可以順利運行。他們建議的解決方案只是洗掉點擊手勢,因為在大多數情況下他們不想要它并且它不小心在那里但是我確實想要點擊手勢所以我無法洗掉它。這就是我在 viewDidLoad() 中添加一個簡單的點擊手勢的方式:
let tapAddImage = UITapGestureRecognizer(target: self, action: #selector(self.addImage(_:)))
tapAddImage.cancelsTouchesInView = false
svAddImage.addGestureRecognizer(tapAddImage)
此功能打開影像選擇器。
此外,我剛剛注意到在其他 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 或點擊手勢在任何其他專案中都沒有問題。知道僅在我使用輕按手勢打開相機或畫廊的這些頁面中可能導致延遲的原因是什么嗎?這僅發生在我單擊按鈕以打開具有輕按手勢的這些 ViewController 的那些 ViewController 上。
uj5u.com熱心網友回復:
請嘗試在 ViewDidAppear 中初始化點擊手勢。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let tapAddImage = UITapGestureRecognizer(target: self, action: #selector(self.addImage(_:)))
tapAddImage.cancelsTouchesInView = false
svAddImage.addGestureRecognizer(tapAddImage)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515427.html
標籤:IOS迅速代码uikituiimagepicker控制器
