注意: ImageView Click事件在Android中可用,我想知道它在iOS中是否可用
我是iOS的新手,请考虑,我有相机图像,必须单击它的OnClick才能打开设备相机,但是我正在获得图像操作功能。附加屏幕截图
我的代码:
import UIKit
class AttendanceViewController: UIViewController {
@IBOutlet weak var loginimagebtn: UIImageView!
@IBOutlet weak var loginstatus: UITextView!
@IBOutlet weak var displayloginimg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
func openCamer() {
if UIImagePickerController.isSourceTypeAvailable(
UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker,animated: true,completion : nil)
}
}
// For Toast message below code
func showToast(message : String) {
let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
}
答案 0 :(得分:6)
您可以使用 UITapGestureRecognizer 在UIImageView上实现此功能。 在您的 viewDidLoad()方法中使用以下代码。
override func viewDidLoad() {
super.viewDidLoad()
let tapGetsure = UITapGestureRecognizer(target: self,
action: #selector(self.openCamer))
tapGetsure.numberOfTapsRequired = 1
loginimagebtn.gestureRecognizers = tapGetsure
loginimagebtn.isUserInteractionEnabled = true
}
答案 1 :(得分:5)
好的,请执行以下步骤
步骤-1
将UIbutton拖放到当前的viewcontroller
步骤-2
删除按钮的默认标题
第3步
UIbutton具有属性图像->将图像添加到相机中,如下图所示,并根据需要最终更改按钮框
步骤-4
转到连接检查器->选择 Touch Up Inside 中的相机按钮事件,并为您的按钮创建事件,例如
选项-2
如果您想继续进行imageview,我的意思是您不想继续进行UIButton,请遵循以下代码
let cameraTap = UITapGestureRecognizer(target: self, action: #selector(cameraTapped))
displayloginimg.isUserInteractionEnabled = true
displayloginimg.addGestureRecognizer(cameraTap)
并为您的imageview处理事件
@objc func cameraTapped() {
// open your camera controller here
}