ra1028/Carbon

Click to enlarge the Image Content?

yo1995 opened this issue · 3 comments

Checklist

Expected Behavior

Would you like to add a tap gesture or so, so that when tapping on imageview content, the image view will zoom in or pop up in a larger view to preview?

currently I embedded the Lightbox framework to it, but wonder if Carbon can provide a native method for it. 🤔

Hi @yo1995 ,

Carbon, like frameworks such as IGListKit, doesn't support complex features other than rendering list elements.
As you say, you can implement it by vanilla implementation or using a framework such as LightBox.
Below is the sample code for receiving events of component on view controller.

final class CustomImageView: UIImageView {
    var onPreview: ((UIImage) -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()

        let gesture = UITapGestureRecognizer(target: self, action: #selector(handlePreview))
        addGestureRecognizer(gesture)
        isUserInteractionEnabled = true
    }

    @objc func handlePreview() {
        guard let image = image else { return }
        onPreview?(image)
    }
}

struct ImageComponent: Component {
    var image: UIImage
    var onPreview: (UIImage) -> Void

    func renderContent() -> CustomImageView {
        return UIImageView()
    }

    func render(in content: CustomImageView) {
        content.image = image
        content.onPreview = onPreview
    }
}

let component = ImageComponent(image: yourImage, onPreview: { image in
    // Implement UIImage previewing
})

Hi @yo1995 ,

Carbon, like frameworks such as IGListKit, doesn't support complex features other than rendering list elements.
As you say, you can implement it by vanilla implementation or using a framework such as LightBox.
Below is the sample code for receiving events of component on view controller.

final class CustomImageView: UIImageView {
    var onPreview: ((UIImage) -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()

        let gesture = UITapGestureRecognizer(target: self, action: #selector(handlePreview))
        addGestureRecognizer(gesture)
        isUserInteractionEnabled = true
    }

    @objc func handlePreview() {
        guard let image = image else { return }
        onPreview?(image)
    }
}

struct ImageComponent: Component {
    var image: UIImage
    var onPreview: (UIImage) -> Void

    func renderContent() -> CustomImageView {
        return UIImageView()
    }

    func render(in content: CustomImageView) {
        content.image = image
        content.onPreview = onPreview
    }
}

let component = ImageComponent(image: yourImage, onPreview: { image in
    // Implement UIImage previewing
})

Thanks for your clarification! Got it.