展开UITableViewCell UIImage

时间:2018-07-03 06:14:51

标签: ios swift uitableview uiimageview

我有一个UITableViewCell,带有两个图像,我的目标是在用户长时间按下后扩展这些图像。在最佳情况下,图像会以小的“ x”或类似的东西覆盖整个屏幕。

我在自定义UITableViewCell中使用了以下功能,但是图像只会扩展单元格的大小。我不知道如何在超级视图的整个tableview / navBar / tabbar上扩展图像。

@objc func answerOneLongPress(_ sender: UILongPressGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height
    newImageView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
    newImageView.backgroundColor = .black
    newImageView.contentMode = .scaleAspectFit
    self.addSubview(newImageView)
}

如果您需要更多信息,请告诉我。我觉得应该在UITableViewController中而不是在单元格中发生这种情况,但是还无法使其以这种方式工作。

1 个答案:

答案 0 :(得分:1)

您不应将视图添加到单元格,而应添加到视图控制器或键窗口。这取决于您的需求。在这种情况下,发生的情况是您的图像视图已添加到单元格上并被裁剪,并且位置也不正确。

我将使用某种对象来处理此图像的呈现。让代码说明一切:

class ImageOverlayController {

    private var startFrame: CGRect
    private var backgroundView: UIView
    private var imageView: UIImageView

    private init(startFrame: CGRect, backgroundView: UIView, imageView: UIImageView) {
        self.startFrame = startFrame
        self.backgroundView = backgroundView
        self.imageView = imageView
    }

    private convenience init() { self.init(startFrame: .zero, backgroundView: UIView(), imageView: UIImageView())  }

    static func showPopupImage(inController viewController: UIViewController? = nil, fromImageView imageView: UIImageView) -> ImageOverlayController {
        guard let targetView = viewController?.view ?? UIApplication.shared.keyWindow else { return ImageOverlayController() } // This should never happen

        let startFrame = imageView.convert(imageView.bounds, to: targetView)

        let backgroundView: UIView = {
            let view = UIView(frame: targetView.bounds)
            view.backgroundColor = UIColor.black.withAlphaComponent(0.0)
            return view
        }()

        let newImageView: UIImageView = {
            let view = UIImageView(frame: startFrame)
            view.image = imageView.image
            return view
        }()

        let controller = ImageOverlayController(startFrame: startFrame, backgroundView: backgroundView, imageView: imageView)

        backgroundView.addSubview(newImageView)
        targetView.addSubview(backgroundView)

        UIView.animate(withDuration: 0.3) {
            backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
            newImageView.frame = targetView.bounds
        }

        return controller

    }

    func dimiss(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.3, animations: {
            self.imageView.frame = self.startFrame
            self.backgroundView.backgroundColor = self.backgroundView.backgroundColor?.withAlphaComponent(0.0)
        }) { _ in
            self.backgroundView.removeFromSuperview()
            completion?()
        }
    }

}

正如您所说,必须仍然添加一个按钮,然后可以在视图上调用dismiss。

注意:我提供的代码没有经过真正的测试,只是很快地组合在一起。让我知道是否有任何问题,因此请对其进行修改。