HomeiOS Developmentios - Resizing UIImageViewFrame to match dimension of picture

ios – Resizing UIImageViewFrame to match dimension of picture


I’ve the next view setup:

non-public func setupViews() {
    let containerView = UIView()
    containerView.clipsToBounds = true
    containerView.backgroundColor = .grey

    view.addSubview(containerView)

    containerView.body.dimension = CGSize(width: 300.0, top: 100.0)
    containerView.middle = view.middle

    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.picture = UIImage(named: "one")

    imageView.body = CGRect(origin: .zero, dimension: CGSize(width: 300.0, top: 100.0))
    containerView.addSubview(imageView)

    let panGesture = UIPanGestureRecognizer(goal: self, motion: #selector(handlePan))
    imageView.addGestureRecognizer(panGesture)
    imageView.isUserInteractionEnabled = true
}

@objc func handlePan(_ gesture: UIPanGestureRecognizer) {
    guard let imageView = gesture.view as? UIImageView else {
        return
    }

    let translation = gesture.translation(in: view)

    let newX = imageView.middle.x + translation.x
    let newY = imageView.middle.y + translation.y

    imageView.middle = CGPoint(x: newX, y: newY)
    gesture.setTranslation(.zero, in: view)
}

It consists of a container view of width: 300.0 and top: 100.0, and a UIImageView of the identical dimensions inserted as a subview. A pan gesture can also be added.

When the code is run the structure is as anticipated. A bigger picture that does not match the body will scale utilizing the content material mode specified, which additionally works as anticipated. Nonetheless I’m solely in a position to pan the picture throughout the body width: 300.0 and top: 100.0. How can I resize the picture view body so it matches that of the resized picture?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments