Swift 手势识别器对点击没有响应
我是新手 学习苹果公司的教程 我做任何事都是当向导的。我知道它已经过时了。 但为什么手势识别器不响应点击 我在“启用用户交互”中添加了一个复选标记 告诉我在这段代码中要修复什么,以便手势识别器对点击做出响应Swift 手势识别器对点击没有响应,swift,uikit,Swift,Uikit,我是新手 学习苹果公司的教程 我做任何事都是当向导的。我知道它已经过时了。 但为什么手势识别器不响应点击 我在“启用用户交互”中添加了一个复选标记 告诉我在这段代码中要修复什么,以便手势识别器对点击做出响应 //MARK: UIImagePickerControllerDelegate func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo i
//MARK: UIImagePickerControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
// MARK: Actions
@IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .photoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
}
您可能没有/丢失与
@iAction
的连接。你需要再次连接它。从情节提要中找到UIAppgestureRecognitizer
,并将控件拖动到UIViewController
子类中,再次创建操作,在其中添加代码并删除旧方法
UIViewController
ViewController
override func viewDidLoad() {
super.viewDidLoad()
tappableView.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(selectImageFromPhotoLibrary(_:)))
tappableView.addGestureRecognizer(tapGesture)
}