我试着在用户点击的时候分配一个UIImageView给一个动作。
我知道如何为一个UIButton创建一个动作,但我怎么能模仿一个UIButton相同的行为,但使用一个UIImageView?
我试着在用户点击的时候分配一个UIImageView给一个动作。
我知道如何为一个UIButton创建一个动作,但我怎么能模仿一个UIButton相同的行为,但使用一个UIImageView?
当前回答
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")
}
其他回答
你需要添加一个手势识别器(对于点击使用UITapGestureRecognizer,对于点击和按住使用UILongPressGestureRecognizer)到你的UIImageView。
let tap = UITapGestureRecognizer(target: self, action: #selector(YourClass.tappedMe))
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
并像这样实现selector方法:
@objc func tappedMe()
{
println("Tapped on Image")
}
你需要一个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)
你可以在UIImageView上面放一个带有透明背景的UIButton,并在加载图像之前监听按钮上的点击
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")
}
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//ViewController is your current view controller
let tap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tappedMe))
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
}
// Need to ass objective-c annotation
@objc
func tappedMe() {
print("Tapped on Image")
}