簡體   English   中英

On long press, how do you call an ImagePreviewController: UIViewController from an objective c function instead of a contextMenuInteraction function?

[英]On long press, how do you call an ImagePreviewController: UIViewController from an objective c function instead of a contextMenuInteraction function?

下面這個 function 需要將 go 改為 ObjC function

///this 1st func is in UITableViewController, the others are in UITableViewCell

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

          if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? ViewControllerTableViewCell {

 ...}

 class ViewControllerTableViewCell: UITableViewCell, UIContextMenuInteractionDelegate

func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    UIContextMenuConfiguration(identifier: nil, previewProvider: {
   if self.cc == interaction  {
            let image3 = UIImage(named:"ringy.png")
            if let unwrappedImage1 = image3 {
                return ImagePreviewController(image:unwrappedImage1) 
            }
            else {
                return nil
            }

        }
        else if self.vv == interaction{
            let image3 = UIImage(named:"green.png")
            if let unwrappedImage1 = image3 {
                return ImagePreviewController(image:unwrappedImage1)   
            }
            else {
                return nil
            }
        }

        else {
                      return nil
                  }
            })
            }

現在對象 C function

    @objc func didLongPress() {
  ///database call
            if ab as! Int>0 {
 /// here the part for ringy.png needs to go
            } else {
  /////here the part for green.png needs to go
            }
        }
        else {
            print("No data available")
        }
    })
}

ObjC 在覆蓋 function 中獲取句柄

   let longPress = UILongPressGestureRecognizer(target: self, action: #selector(didLongPress))
    like?.addGestureRecognizer(longPress)

我目前遇到什么錯誤:

非空中空 function。 也就是說,如果我將代碼的 ringy/image3 和 green/image3 部分放入 objC function。

第一次回答后更新

我似乎可以使用答案和一些修改

  1. weak var viewController: UIViewController?
  2.  viewController?.present(previewVC, animated: true, completion: nil)
  3.  cell.viewController = self ///inside cellForRowAt

我剩下的唯一問題是 ImagePreviewController 的尺寸錯誤/幾乎全屏。 他們應該是:

  class ImagePreviewController: UIViewController {
private let imageView = UIImageView()
init(image: UIImage) {
    super.init(nibName: nil, bundle: nil)
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true
    imageView.image = image
    view = imageView
    let size = UIScreen.main.bounds.size
    preferredContentSize = .init(width: size.width, height: size.height/1.55)
}
required init?(coder: NSCoder) {
    super.init(coder: coder)
}

如果您想呈現一個新視圖 controller,您可以執行 segue(如果您使用故事板)或使用經典方式:

@objc func didLongPress() {
    var image:UIImage?
    // Some database call? How to get "ab"?
    // Maybe you should not use as! in the following check:
    if ab as! Int>0 {
        image = UIImage(named:"ringy.png")
    } else {
        image = UIImage(named:"green.png")
    }
        
    if let image = image {
        let previewVC = ImagePreviewController(image:image)
        self.present(previewVC, animated: true, completion: nil)
    } else {
        print("No data available")
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM