我是swift的新手(以及一般的Xcode开发),我想知道如何使故事板上的ImageView可点击。我正在尝试做的是,当它点击它时,它显示另一个视图控制器。
答案 0 :(得分:58)
您可以为此添加点按手势。这是代码:
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
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
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
}
}
}
答案 1 :(得分:20)
您可以更轻松,并通过故事板点击图片,而无需编码。
UITapGestureRecognizer
拖到故事板中的UIImageView
。@IBAction func imageClicked(_ sender: Any) {}
UITapGestureRecognizer
中选择手势识别器,然后切换到IBAction
并拖动{Document Outline
,将Connection Inspector Tab
连接到班级中的Sent Actions
{1}} - > Selector
UIViewController
到User Interaction Enabled
,然后您可以选择之前创建的相应操作。Triggered Segues
。 完成,一个完全可点击的UIImageView,除了你要调用的明显函数外,没有编写任何代码。但是,嘿,如果你想要推动一个segue,那么你可以通过使用手势识别器Sent Actions
而不是writerow
来完成编码。
尽管Storyboard有其局限性,但无需为可点击图像编写代码。 ;)
答案 2 :(得分:5)
我建议创建一个没有文本的UIButton,并将其设为您想要的图像。执行此操作后,您可以从图像按CTRL拖动到要切换到的视图控制器。或者您可以在视图控制器的代码中进行IBAction手动分段。
答案 3 :(得分:4)
for swift version 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")
}
答案 4 :(得分:2)
在故事板上设置图像视图,启用用户交互,然后使用此方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == yourImageView)
{
//add your code for image touch here
}
}
答案 5 :(得分:1)
我通过设置用户互动enable = true
以及下面的代码在TouchesBegan中......干净而简单
ImageView也在StackView
中if let touch = touches.first {
if touch.view == profilePicture {
print("image touched")
}
}
答案 6 :(得分:0)
好吧,尽管您可以像上面提到的那样使用UITapGestureRecognizer,但是如果您需要做一个快速的项目并且只关心应用程序的功能,最简单的方法是使用一个透明的按钮覆盖UIImageView,并且不显示任何文本,并且然后使用该代码编写所需的任何代码。当然,如果您正在做一个非常重要的项目或其他事情,则不建议这样做,但是如果您只想制作一个快速的应用程序(例如,某个会议的简单MVP),此方法应该可以正常工作。
答案 7 :(得分:0)
首先,创建一个图像视图。比写这段代码覆盖 func viewDidLoad() { super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(tapGesture))
imageview.addGestureRecognizer(tap)
imageview.isUserInteractionEnabled = true
}
func tapGesture() {
print("Image View Tapped")
}
在您的视图控制器中。
UITapGestureRecognizer 方法做的一切都是可点击的