我在问是否可以使用gestureRecognizer设置UIImageView的图像,或者是否需要在UIImageView的顶部覆盖按钮。
我有一个UIImageViews的插座集合,名为imageViews。其中有四个,它们的标签已经设置为1到4.在我的viewDidLoad中,要将手势识别器添加到集合中的每个图像视图中,我使用过:
override func viewDidLoad() {
super.viewDidLoad()
for i in (0..<imageViews.count) {
let imageViewTapped = UITapGestureRecognizer(target: self, action: #selector(selectImage(tap:)))
imageViewTapped.numberOfTapsRequired = 1
imageViews[i].addGestureRecognizer(imageViewTapped)
}
}
接下来,我将gestureRecognizer创建为viewController类中的一个函数。这是创建图像选择器控制器的位置,以及识别被轻击的图像视图的位置:
func selectImage(tap: UITapGestureRecognizer) {
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true, completion: nil)
guard let viewTappedTag = tap.view?.tag else {return}
self.selectedImageView = imageViews[viewTappedTag - 1]
}
selectedImageView是viewController类中的一个变量,其类型为UIImageView。我的想法是,这个变量可以保存被轻击的UIImageView,它由gestureRecognizer识别,如上所示。然后可以稍后将其传递给代理。
接下来,我创建了委托,didFinishPickingMediaWithInfo和didCancel,尽管我只是为了简洁而展示前者。两者都是在viewController类中创建的:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var chosenImage = UIImage()
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.selectedImageView.image = chosenImage
dismiss(animated: true, completion: nil)
}
不幸的是,被点击的图像视图的图像未更新为从库中选择的图像。这样做的正确方法是什么?而且我觉得我的viewController充满了所有这些代码,这可以转移到一个单独的类吗?这样做我错了,而应该只是覆盖一个按钮?
修改
到目前为止我能想到的最好的是我的imagePickerController中的switch语句,其中imageViewOne,imageViewTwo等是每个UIImageView的出口:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var chosenImage = UIImage()
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
switch self.viewTappedTag {
case 1:
imageViewOne.image = chosenImage
case 2:
imageViewTwo.image = chosenImage
case 3:
imageViewThree.image = chosenImage
case 4:
imageViewFour.image = chosenImage
default:
imageViewOne.image = chosenImage
}
dismiss(animated: true, completion: nil)
}
其中viewTappedTag是一个viewController类变量,其值由被轻击并在gestureRecognizer中设置的视图的标记定义。任何人都可以改进吗?
答案 0 :(得分:0)
按以下方式更新代码
在类中声明一个变量,如下所示
var selectedImageViewIndex = 0 //by default selected image view is 0th image view
func selectImage(tap: UITapGestureRecognizer) {
var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(imagePicker, animated: true, completion: nil)
guard let viewTappedTag = tap.view?.tag else {return}
self.selectedImageView = imageViews[viewTappedTag - 1]
//Add the following line
self.selectedImageViewIndex = viewTappedTag - 1
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var chosenImage = UIImage()
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.selectedImageView.image = chosenImage
//Add the following line
self.imageView[self.selectedImageViewIndex].image = chosenImage
dismiss(animated: true, completion: nil)
}
虽然我建议不要使用viewTappedTag - 1,而是从映射对象中获取index和tapped image标签的值。
答案 1 :(得分:0)
最后我使用switch语句来实现我的目标:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var chosenImage = UIImage()
chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
switch self.viewTappedTag {
case 1:
imageViewOne.image = chosenImage
case 2:
imageViewTwo.image = chosenImage
case 3:
imageViewThree.image = chosenImage
case 4:
imageViewFour.image = chosenImage
default:
imageViewOne.image = chosenImage
}
dismiss(animated: true, completion: nil)
}