如何在iOS中创建imageView点击事件?

Gow*_*n M 0 uiimageview ios swift

注意: ImageView Click事件在Android中可用,我想知道它是否在iOS中可用

我是iOS新手,请考虑,我有相机图像,OnClick它我必须打开我的设备相机,但我正在获取Action的图像功能.附上截图

样品

我的代码:

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()
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

iOS*_*cus 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
    }
Run Code Online (Sandbox Code Playgroud)


Anb*_*hik 5

好的,请执行以下步骤

步骤1

将UI按钮拖放到当前viewcontroller

第2步

删除按钮的默认标题

在此输入图像描述

第3步

UI按钮具有属性图像 - >转到图像添加您的相机图像如下,最后根据您的需要更改按钮框架

在此输入图像描述

第4步

转到连接Inspector - >选择Touch Up Inside的相机按钮事件,并为您的按钮创建事件,例如

在此输入图像描述

选项 - 2

如果你想继续你的imageview我的意思是你不想继续UIButton然后按照这个代码

 let cameraTap = UITapGestureRecognizer(target: self, action: #selector(cameraTapped))
    displayloginimg.isUserInteractionEnabled = true
    displayloginimg.addGestureRecognizer(cameraTap)
Run Code Online (Sandbox Code Playgroud)

并为您的imageview处理事件

 @objc func cameraTapped() {
   // open your camera controller here
 }
Run Code Online (Sandbox Code Playgroud)