如何在 Swift 中为 UIImageView 对象分配动作
How to assign an action for UIImageView object in Swift
我正在尝试将 UIImageView
分配给用户点击操作时的操作。
我知道如何为 UIButton
创建一个动作,但我如何模仿 UIButton
的相同行为,但使用 UIImageView
?
您需要为您的 UIImageView
添加一个手势识别器(用于点击 UITapGestureRecognizer, for tap and hold use UILongPressGestureRecognizer)。
let tap = UITapGestureRecognizer(target: self, action: #selector(YourClass.tappedMe))
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
并像这样实现选择器方法:
@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
)
您实际上可以将 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)
告诉您的用户您正在加载图片
您可以添加一个 UITapGestureRecognizer
到 imageView,只需将一个拖入您的 Storyboard/xib,Ctrl-drag 从 imageView 拖到 gestureRecognizer,Ctrl-drag 从 gestureRecognizer到 Swift-file 做一个 IBAction
.
您还需要在 UIImageView
上启用用户交互,如下图所示:
您可以在 UIImageView
上方放置一个具有透明背景的 UIButton
,并在加载图像之前聆听点击按钮的声音
对于 Swift 执行此操作:
@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.isUserInteractionEnabled = true
}
func tappedMe() {
print("Tapped on Image")
}
Swift4码
ViewdidLoad() 中的第 1 步
let pictureTap = UITapGestureRecognizer(target: self, action: #selector(MyInfoTableViewController.imageTapped))
userImageView.addGestureRecognizer(pictureTap)
userImageView.isUserInteractionEnabled = true
第 2 步添加以下功能
@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
}
它已经过测试并且工作正常
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")
}
我建议将 invisible(opacity = 0) 按钮放在您的 imageview 上,然后处理按钮上的交互。
需要为TapGestureRecognizer添加lazy才能注册
因为 UITapGestureRecognizer(target: self ...) 中的 'self' 如果不是惰性变量,则将为 nil。即使您设置 isUserInteractionEnable = true,如果没有惰性变量,它也不会注册。
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")
}
这是我发现的,设置起来更容易。
- 打开库,查找“点击手势识别器”对象。
- 将对象拖到情节提要中,然后将委托设置为要触发动作的图像。
- 然后转到视图控制器,拖动相同的对象以设置 IBAction。
@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")
}
我正在尝试将 UIImageView
分配给用户点击操作时的操作。
我知道如何为 UIButton
创建一个动作,但我如何模仿 UIButton
的相同行为,但使用 UIImageView
?
您需要为您的 UIImageView
添加一个手势识别器(用于点击 UITapGestureRecognizer, for tap and hold use UILongPressGestureRecognizer)。
let tap = UITapGestureRecognizer(target: self, action: #selector(YourClass.tappedMe))
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
并像这样实现选择器方法:
@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
)
您实际上可以将 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)
告诉您的用户您正在加载图片
您可以添加一个 UITapGestureRecognizer
到 imageView,只需将一个拖入您的 Storyboard/xib,Ctrl-drag 从 imageView 拖到 gestureRecognizer,Ctrl-drag 从 gestureRecognizer到 Swift-file 做一个 IBAction
.
您还需要在 UIImageView
上启用用户交互,如下图所示:
您可以在 UIImageView
上方放置一个具有透明背景的 UIButton
,并在加载图像之前聆听点击按钮的声音
对于 Swift 执行此操作:
@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.isUserInteractionEnabled = true
}
func tappedMe() {
print("Tapped on Image")
}
Swift4码
ViewdidLoad() 中的第 1 步
let pictureTap = UITapGestureRecognizer(target: self, action: #selector(MyInfoTableViewController.imageTapped))
userImageView.addGestureRecognizer(pictureTap)
userImageView.isUserInteractionEnabled = true
第 2 步添加以下功能
@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
}
它已经过测试并且工作正常
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")
}
我建议将 invisible(opacity = 0) 按钮放在您的 imageview 上,然后处理按钮上的交互。
需要为TapGestureRecognizer添加lazy才能注册
因为 UITapGestureRecognizer(target: self ...) 中的 'self' 如果不是惰性变量,则将为 nil。即使您设置 isUserInteractionEnable = true,如果没有惰性变量,它也不会注册。
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")
}
这是我发现的,设置起来更容易。
- 打开库,查找“点击手势识别器”对象。
- 将对象拖到情节提要中,然后将委托设置为要触发动作的图像。
- 然后转到视图控制器,拖动相同的对象以设置 IBAction。
@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")
}