【发布时间】:2015-03-23 03:33:42
【问题描述】:
我是 swift 新手(以及一般的 Xcode 开发),我想知道如何使情节提要板上的 ImageView 可点击。我想做的是让它在点击时显示另一个视图控制器。
【问题讨论】:
标签: ios swift uiimageview
我是 swift 新手(以及一般的 Xcode 开发),我想知道如何使情节提要板上的 ImageView 可点击。我想做的是让它在点击时显示另一个视图控制器。
【问题讨论】:
标签: ios swift uiimageview
您可以为此添加 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
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
}
}
}
【讨论】:
UIButton 比使图像可点击要好得多。添加辅助功能选项时,您会感谢我的。
您可以做到更简单,并通过故事板制作可点击的图像完全无需编码。
UITapGestureRecognizer 拖到 Storyboard 中的 UIImageView 上。@IBAction func imageClicked(_ sender: Any) {} 创建要在代码中运行的 IBAction
UITapGestureRecognizer 连接到班级中的IBAction,方法是在Document Outline 中选择手势识别器,然后切换到Connection Inspector Tab 并拖动Sent Actions -> Selector 到您的UIViewController,然后您可以在其中选择您之前创建的相应操作。User Interaction Enabled。完成,一个完全可点击的 UIImageView,除了您要调用的明显功能外,无需编写任何代码。但是,嘿,例如,如果您想要推送一个 segue,那么您可以通过使用手势识别器 Triggered Segues 而不是它的 Sent Actions 来完全不编码。
虽然 Storyboard 有其局限性,但无需为可点击的图像编写代码。 ;)
【讨论】:
viewDidLoad() 中明确设置:fooImage.isUserInteractionEnabled = true 才能使其工作(Xcode 错误?还是什么?我不知道)
我建议创建一个没有文本的 UIButton 并将其设置为您想要的图像。完成此操作后,您可以按住 CTRL 键从图像拖动到要继续的视图控制器。或者你可以在你的视图控制器的代码中创建一个手动 segues 的 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),则此方法应该可以正常工作。
【讨论】:
首先,创建一个图像视图。比写这段代码覆盖 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 方法做的一切都是可点击的
【讨论】:
在我看来,合理的方法是创建 UIImage 的扩展...
这是我找到的代码...
public typealias SimpleClosure = (() -> ())
private var tappableKey : UInt8 = 0
private var actionKey : UInt8 = 1
extension UIImageView {
@objc var callback: SimpleClosure {
get {
return objc_getAssociatedObject(self, &actionKey) as! SimpleClosure
}
set {
objc_setAssociatedObject(self, &actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
var gesture: UITapGestureRecognizer {
get {
return UITapGestureRecognizer(target: self, action: #selector(tapped))
}
}
var tappable: Bool! {
get {
return objc_getAssociatedObject(self, &tappableKey) as? Bool
}
set(newValue) {
objc_setAssociatedObject(self, &tappableKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
self.addTapGesture()
}
}
fileprivate func addTapGesture() {
if (self.tappable) {
self.gesture.numberOfTapsRequired = 1
self.isUserInteractionEnabled = true
self.addGestureRecognizer(gesture)
}
}
@objc private func tapped() {
callback()
}
}
用法应该是这样的
@IBOutlet weak var exampleImageView: UIImageView! {
didSet {
self.exampleImageView.tappable = true
}
}override func viewDidLoad() {
super.viewDidLoad()
self.exampleImageView.callback = {
//TODO: Here you put the On click code.
}
}
【讨论】: