Search code examples
swifttagsimageviewuipangesturerecognizercgpoint

use 2 different UIPanGestureRecongizer on 2 different imageviews in a viewController


my code below initializes 2 different imageviews pg and tim. I have create a seperate var for the UIPanGestureRecognizer to be used for each imageview and func to move the imageview around using UIPanGestureRecongizer. Right now I can not move the imageviews around. When I click on pg it disapers. I just want to develop a function that allows me to move each imageview around freely. All of my code is programmatically so you can just copy the code.

import UIKit

class ViewController: UIViewController {

var pg = UIImageView()
var pgGesture = UIPanGestureRecognizer()
var pgCon = [NSLayoutConstraint]()

var tim = UIImageView()
var timGesture = UIPanGestureRecognizer()
var timCon = [NSLayoutConstraint]()



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.



    [pg,tim].forEach({
        $0.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview($0)
        $0.backgroundColor = UIColor.systemPink
        $0.isUserInteractionEnabled = true
    })


    pgGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.pgGestureMethod(_:)))
    pg.addGestureRecognizer(pgGesture)


    timGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.timGestureMethod(_:)))
    tim.addGestureRecognizer(timGesture)









    pgCon = [

        pg.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :37.5),
        pg.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
        pg.widthAnchor.constraint(equalToConstant: 75),
        pg.heightAnchor.constraint(equalToConstant: 50),
    ]


    NSLayoutConstraint.activate(pgCon)

    timCon = [

        tim.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :120),
        tim.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
        tim.widthAnchor.constraint(equalToConstant: 75),
        tim.heightAnchor.constraint(equalToConstant: 50),
    ]


    NSLayoutConstraint.activate(timCon)






}

@objc func pgGestureMethod(_ sender: UIPanGestureRecognizer){
    NSLayoutConstraint.deactivate(pgCon)
    NSLayoutConstraint.deactivate(timCon)
    self.view.bringSubviewToFront(pg)
    let tranistioon = sender.translation(in: self.view)
    pg.center = CGPoint(x: pg.center.x + tranistioon.x, y: pg.center.y + tranistioon.y)
    sender.setTranslation(CGPoint.zero,in: self.view)


}
@objc func timGestureMethod(_ sender: UIPanGestureRecognizer){
    NSLayoutConstraint.deactivate(timCon)
    NSLayoutConstraint.deactivate(pgCon)
    self.view.bringSubviewToFront(tim)
    let tranistioon = sender.translation(in: self.view)
    tim.center = CGPoint(x: tim.center.x + tranistioon.x, y: tim.center.y + tranistioon.y)
    sender.setTranslation(CGPoint.zero,in: self.view)


}
}

Solution

  • Instead of constraints, try giving the frame to both imageViews, i.e.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        pg.frame = CGRect(x: 100, y: 100, width: 75, height: 50) //here....
        tim.frame = CGRect(x: 200, y: 200, width: 75, height: 50) //here....
    
        //rest of the code...
    }
    

    In the above code,

    1. change the frame as per your requirement.
    2. from viewDidLoad() remove the code for applying constraints.