ios 你如何使故事板上的 UIImageView 可点击(快速)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29202882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:18:11  来源:igfitidea点击:

How do you make an UIImageView on the storyboard clickable (swift)

iosswiftuiimageview

提问by pjtnt11

I am new to swift (and Xcode development in general) and I was wondering how to make an ImageView on the storyboard clickable. What I'm trying to do is make it so when its clicked, it shows another view controller.

我是 swift 的新手(以及一般的 Xcode 开发),我想知道如何使故事板上的 ImageView 可点击。我想要做的是让它在点击时显示另一个视图控制器。

回答by Dharmesh Kheni

You can add tapGesture for that. Here is the code:

您可以为此添加 tapGesture。这是代码:

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    // create tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")

    // add it to the image view;
    imageView.addGestureRecognizer(tapGesture)
    // make sure imageView can be interacted with by user
    imageView.userInteractionEnabled = true
}

func imageTapped(gesture: UIGestureRecognizer) {
    // if the tapped view is a UIImageView then set it to imageview
    if let imageView = gesture.view as? UIImageView {
        println("Image Tapped")
        //Here you can initiate your new ViewController

        }
    }
}

Swift 3.0

斯威夫特 3.0

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped(gesture:)))

        // add it to the image view;
        imageView.addGestureRecognizer(tapGesture)
        // make sure imageView can be interacted with by user
        imageView.isUserInteractionEnabled = true
    }

    func imageTapped(gesture: UIGestureRecognizer) {
        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            print("Image Tapped")
            //Here you can initiate your new ViewController

        }
    }
}

Swift 5.0

斯威夫特 5.0

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped(gesture:)))

        // add it to the image view;
        imageView.addGestureRecognizer(tapGesture)
        // make sure imageView can be interacted with by user
        imageView.isUserInteractionEnabled = true
    }

    @objc func imageTapped(gesture: UIGestureRecognizer) {
        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            print("Image Tapped")
            //Here you can initiate your new ViewController

        }
    }
}

回答by xxtesaxx

You can do it even easierand make a image clickable via Storyboard with no coding at all.

您可以更轻松地通过 Storyboard 制作可点击的图像,而无需编码

  • First you need to drag a UITapGestureRecognizeronto your UIImageViewin Storyboard.
  • Then you create the IBAction you want to run in your code with @IBAction func imageClicked(_ sender: Any) {}
  • Next you need to connect the UITapGestureRecognizerto the IBActionin your class by selecting the gesture recognizer in the Document Outline, then switching to the Connection Inspector Taband dragging the Sent Actions-> Selectorto your UIViewControllerwhere you then select the appropriate action you created previously.
  • Finally you have to set the checkbox User Interaction Enabledon the image view.
  • 首先,您需要将 aUITapGestureRecognizer拖到您UIImageView的故事板中。
  • 然后创建要在代码中运行的 IBAction @IBAction func imageClicked(_ sender: Any) {}
  • 接下来,您需要通过选择 中的手势识别器将 连接UITapGestureRecognizerIBAction类中的Document Outline,然后切换到Connection Inspector Tab并将Sent Actions->拖到Selector您的UIViewController位置,然后选择您之前创建的适当操作。
  • 最后,您必须User Interaction Enabled在图像视图上设置复选框。

Done, a fully clickable UIImageView without writing a single line of code except the obvious function you want to invoke. But hey, if you for example want to push a segue instead, then you can go without coding at all by using the gesture recognizers Triggered Seguesinstead of its Sent Actions.

完成了,一个完全可点击的 UIImageView,除了你想要调用的明显功能外,无需编写任何代码。但是,嘿,例如,如果您想改为推送 segue,那么您可以通过使用手势识别器Triggered Segues而不是其Sent Actions.

Though Storyboard have their limitations, theres no need to write code for clickable images. ;)

尽管 Storyboard 有其局限性,但无需为可点击图像编写代码。;)

enter image description here

在此处输入图片说明

回答by Zolnoor

I would suggest creating a UIButton with no text and making it the image you want, instead. After doing that, you can CTRL-drag from the image to the view controller you want to segue to. Or you can just make an IBAction in your view controller's code that manually segues.

我建议创建一个没有文本的 UIButton 并使其成为您想要的图像。完成后,您可以按住 CTRL 键从图像拖动到您想要转接的视图控制器。或者你可以在你的视图控制器的代码中创建一个 IBAction 手动segues。

回答by Rajesh.k

for swift version 3.0 try below code

对于 Swift 3.0 版,请尝试以下代码

override func viewDidLoad() {
super.viewDidLoad()

let tap1 = UITapGestureRecognizer(target: self, action: #selector(tapGesture1))
imageview.addGestureRecognizer(tap1)
imageview.isUserInteractionEnabled = true
}
func tapGesture1() {
    print("Image Tapped")
}

回答by Anish Parajuli ?

On the storyboard set image view user interaction enabled and then get with this method

在故事板上设置图像视图用户交互已启用,然后使用此方法获取

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if ([touch view] == yourImageView)
    {
            //add your code for image touch here 
    }
}

回答by IOSDev

I achieved this by setting user interaction enable = true

我通过设置用户交互来实现这一点 enable = true

and this code below in TouchesBegan... Clean and simple for this

下面这段代码在 TouchesBegan 中......干净而简单

ImageView was also inside a StackView

ImageView 也在 StackView 里面

if let touch = touches.first {
   if touch.view == profilePicture {
      print("image touched")
   }
}

回答by Kyro

Well, although you can use a UITapGestureRecognizer like mentioned above, if you need to do a quick project and just care about the functionality of the app, the easiest way is to have a transparent button covering your UIImageView with no text, and then using that to write whatever code you want. Sure, this will not be recommended if you're making a very important project or something but if you just want to make a quick app (let's say a very simple MVP for some meeting) this method should work fine.

好吧,虽然你可以使用上面提到的 UITapGestureRecognizer,但如果你需要做一个快速的项目并且只关心应用程序的功能,最简单的方法是让一个透明的按钮覆盖你的 UIImageView 没有文本,然后使用它编写任何你想要的代码。当然,如果您正在制作一个非常重要的项目或其他东西,则不建议这样做,但如果您只想制作一个快速的应用程序(假设某个会议的一个非常简单的 MVP),这种方法应该可以正常工作。