是否可以单击imageView并打开新的View Controller?

Van*_*ani 1 uiviewcontroller uiimageview ios swift

我是IOS的新手,目前正在学习IDE.我想知道是否有可能在单击时将imageView链接到View Controller

Vad*_*pov 6

当然.首先,viewDidLoad你的UIViewControllermake to tappable:

imageView.isUserInteractionEnabled = true
Run Code Online (Sandbox Code Playgroud)

然后分配手势识别器:

imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.imageTap)))
Run Code Online (Sandbox Code Playgroud)

触发操作后,执行到新VC的转换:

// Func in your UIViewController
@objc func imageTap() {
    // present modally
    self.present(YourNewViewController())
    // or push to the navigation stack
    self.navigationController?.push(YourNewViewController())
    // or perform segue if you use storyboards
    self.preformSegue(...)
}
Run Code Online (Sandbox Code Playgroud)