adri567
adri567

Reputation: 661

swift pan a imageView that is behind a view

Is it possible to pan an object that is behind another object? Currently I have a imageview and a view with clear color and only a border around, that I can see the imageview. I want to achieve that the view with the border is always on top. Trough the view I want to pan the image view with pan gesture.

Upvotes: 0

Views: 338

Answers (2)

Okan Kocyigit
Okan Kocyigit

Reputation: 13431

Just set isUserInteractionEnabled false for upper view.

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let bottomView = UIView.init(frame: .init(x: 100, y: 100, width: 300, height: 300))
        bottomView.backgroundColor = .yellow

        let upperView = UIView.init(frame: .init(x: 100, y: 100, width: 300, height: 300))
        upperView.backgroundColor = .clear
        upperView.layer.borderWidth = 1
        upperView.layer.borderColor = UIColor.red.cgColor
        upperView.isUserInteractionEnabled = false //<------------

        view.addSubview(bottomView)
        view.addSubview(upperView)

        bottomView.addGestureRecognizer(UIPanGestureRecognizer.init(target: self, action: #selector(handle)))

    }

    @objc func handle() {
        print("handletapgesture")
    }
}

Upvotes: 1

Christopher Robert
Christopher Robert

Reputation: 9

You can add Pan gesture to the view and apply the transformation to the imageView.

var translation = panGesture.translationInView(imageView)
panGesture.setTranslation(CGPointZero, inView: imageView)

Upvotes: 1

Related Questions