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