如何在Swift中为UIImageView对象分配一个动作

时间:2015-01-10 19:59:28

标签: ios swift uiimageview

我正在尝试在用户点击操作时为其分配UIImageView

我知道如何为UIButton创建操作,但我怎样才能模仿UIButton的相同行为,但使用UIImageView

11 个答案:

答案 0 :(得分:359)

您需要UITapGestureRecognizer。 设置使用此:

override func viewDidLoad()
{
    super.viewDidLoad()

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    imageView.isUserInteractionEnabled = true
    imageView.addGestureRecognizer(tapGestureRecognizer)
}

func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
    let tappedImage = tapGestureRecognizer.view as! UIImageView

    // Your action
}

(您也可以使用UIButton并为其分配图像,不使用文本,只需连接IBAction)

答案 1 :(得分:63)

您需要添加一个手势识别器(用于点按使用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")
}

答案 2 :(得分:33)

您可以向imageView添加UITapGestureRecognizer,只需将一个拖到Storyboard / xib中,然后从imageView拖动到gestureRecognizer,然后按Ctrl-拖动从gestureRecognizer到Swift文件即可IBAction

您还需要在UIImageView上启用用户互动,如下图所示: enter image description here

答案 3 :(得分:13)

对于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")
    }

答案 4 :(得分:11)

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")
        }
    }

}

现在,只要我们想向UITapGestureRecognizerUIView之类的UIViewUIImageView子类中添加 profile_ImageView.addTapGestureRecognizer { print("image tapped") } ,我们都可以这样做,而无需为选择器创建关联函数! >

用法:

gsutil defacl ch -u allUsers:R gs://my-bucket

答案 5 :(得分:8)

您实际上只需将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)

告诉您的用户您正在加载图片

答案 6 :(得分:1)

您可以在UIButton的顶部放置一个透明背景的UIImageView,并在加载图片之前先点按按钮

答案 7 :(得分:1)

需要添加懒惰以便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")
}

答案 8 :(得分:0)

Swift 4代码


步骤1在ViewdidLoad()

<h1>Hello World</h1>
<?php
$custom_script ="
alert('true);
";

wp_add_inline_script('dstg-inline-script', $custom_script, 'before');
?>

第2步添加以下功能

   let pictureTap = UITapGestureRecognizer(target: self, action: #selector(MyInfoTableViewController.imageTapped))
       userImageView.addGestureRecognizer(pictureTap)
       userImageView.isUserInteractionEnabled = true

它已经过测试并且可以正常工作

答案 9 :(得分:0)

我建议在图像视图上放置不可见(不透明度= 0)按钮,然后处理按钮上的交互。

答案 10 :(得分:0)

这是我发现的,更容易设置。

Here is the YouTube video

  1. 打开库,查找“点击手势识别器”对象。
  2. 将对象拖到情节提要上,然后将代理设置为要触发操作的图像。
  3. 然后转到视图控制器,拖动相同的对象以设置 IBAction。