首页 文章

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

提问于
浏览 221
140

当用户点击它时,我正试图将 UIImageView 分配给一个动作 .

我知道如何为 UIButton 创建一个动作,但我怎么能模仿 UIButton 的相同行为,但使用 UIImageView

7 回答

  • 319

    实际上你可以将 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
    }
    

    使用此方法的好处是,如果必须从数据库加载图像,则可以在设置图像之前设置按钮的 Headers :

    myButton.setTitle("Loading Image...", forState: UIControlState.Normal)
    

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

  • 54

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

  • 30

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

    你需要 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)

  • 7

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

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

  • 1

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

    Swift 4 Code


    步骤1在ViewdidLoad()中

    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
    
        }
    

    经过测试和正常工作

相关问题