Answers:
您需要一个UITapGestureRecognizer
。要进行设置,请使用以下命令:
override func viewDidLoad()
{
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let tappedImage = tapGestureRecognizer.view as! UIImageView
// Your action
}
(您还可以使用,UIButton
并为其分配图片,不添加文字,而只需连接即可IBAction
)
@objc
有用,但是我必须在函数中添加修饰符。
您需要在其中添加一个手势识别器(对于轻击,请使用UITapGestureRecognizer;对于轻击并按住,请使用UILongPressGestureRecognizer)UIImageView
。
let tap = UITapGestureRecognizer(target: self, action: #selector(YourClass.tappedMe))
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
并实现选择器方法,例如:
@objc func tappedMe()
{
println("Tapped on Image")
}
您可以将其添加UITapGestureRecognizer
到imageView中,只需将其拖到Storyboard / xib中,将Ctrl从imageView拖动到gestureRecognizer,然后将Ctrl从gestureRecognizer拖动到Swift文件中即可IBAction
。
您还需要在上启用用户交互UIImageView
,如下图所示:
UIImageView
默认情况下不响应触摸。您还必须启用用户交互。
对于Swift 2.0及更高版本,请执行此操作
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tappedMe))
imageView.addGestureRecognizer(tap)
imageView.userInteractionEnabled = true
}
func tappedMe()
{
print("Tapped on Image")
}
Swift4代码
试试这个新的扩展方法:
import UIKit
extension UIView {
fileprivate struct AssociatedObjectKeys {
static var tapGestureRecognizer = "MediaViewerAssociatedObjectKey_mediaViewer"
}
fileprivate typealias Action = (() -> Void)?
fileprivate var tapGestureRecognizerAction: Action? {
set {
if let newValue = newValue {
// Computed properties get stored as associated objects
objc_setAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
get {
let tapGestureRecognizerActionInstance = objc_getAssociatedObject(self, &AssociatedObjectKeys.tapGestureRecognizer) as? Action
return tapGestureRecognizerActionInstance
}
}
public func addTapGestureRecognizer(action: (() -> Void)?) {
self.isUserInteractionEnabled = true
self.tapGestureRecognizerAction = action
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
self.addGestureRecognizer(tapGestureRecognizer)
}
@objc fileprivate func handleTapGesture(sender: UITapGestureRecognizer) {
if let action = self.tapGestureRecognizerAction {
action?()
} else {
print("no action")
}
}
}
现在,每当我们想将添加UITapGestureRecognizer
到UIView
或UIView
子类一样UIImageView
,我们可以这样做而无需选择创建相关的功能!
用法:
profile_ImageView.addTapGestureRecognizer {
print("image tapped")
}
实际上,您可以将的图片设置为UIButton
通常放置在中的图片UIImageView
。例如,在哪里可以做:
myImageView.image = myUIImage
您可以改用:
myButton.setImage(myUIImage, forState: UIControlState.Normal)
因此,代码如下所示:
override func viewDidLoad(){
super.viewDidLoad()
var myUIImage: UIImage //set the UIImage here
myButton.setImage(myUIImage, forState: UIControlState.Normal)
}
@IBOutlet var myButton: UIButton!
@IBAction func buttonTap(sender: UIButton!){
//handle the image tap
}
使用此方法的好处在于,如果必须从数据库中加载图像,则可以在设置图像之前设置按钮的标题:
myButton.setTitle("Loading Image...", forState: UIControlState.Normal)
告诉用户您正在加载图像
需要为TapGestureRecognizer添加懒惰以便注册,
因为如果UITapGestureRecognizer(target:self ...)中的'self'不是惰性变量,则它将为nil。即使您将isUserInteractionEnable = true设置为true,也不会在没有惰性var的情况下进行注册。
lazy var imageSelector : UIImageView = {
let image = UIImageView(image: "imageName.png")
//now add tap gesture
image.isUserInteractionEnabled = true
image.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleImageSelector)))
return image
}()
@objc private func handleImageSelector() {
print("Pressed image selector")
}
您可以UIButton
在的顶部放置一个具有透明背景的UIImageView
,并在加载图像之前先听一下按钮的轻触
let pictureTap = UITapGestureRecognizer(target: self, action: #selector(MyInfoTableViewController.imageTapped))
userImageView.addGestureRecognizer(pictureTap)
userImageView.isUserInteractionEnabled = true
@objc func imageTapped() {
let imageView = userImageView
let newImageView = UIImageView(image: imageView?.image)
newImageView.frame = UIScreen.main.bounds
newImageView.backgroundColor = UIColor.black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true
}